问题
I'm new to C++ and I have a basic doubt.
I'm creating a french verb conjugating application.
I have two files, a Conjugator.cpp file and an ErVerbs.cpp file.
I want to keep the bulk of my functions in the ErVerbs source file and use the conjugator
file to use these functions.
Here are a few code snippets:
Conjugator.cpp
#include <iostream>
#include <string>
#include "Variables.h"
#include "ErVerbs.cpp"
#include "IrVerbs.cpp"
#include "ReVerbs.cpp"
using namespace std;
void check()
{
if (verb.substr(len - 2, len) == "er")
erVerbs();
else if (verb.substr(len - 2, len) == "ir")
irVerbs();
else if (verb.substr(len - 2, len) == "re")
reVerbs();
}
int main()
{
cout << "Enter verb : ";
getline(cin, verb);
cout << "Enter tense : ";
getline(cin, tense);
len = verb.length();
check();
}
ErVerbs.cpp
#include <iostream>
#include <string>
using namespace std;
void erVerbs()
{
cout << "er Verb"; cin.get();
}
Similarly, I have three other such .cpp source files with similar functions.
When I build the program, I get an error that each of the methods I'm using has been defined
already.
1>ErVerbs.obj : error LNK2005: "void __cdecl erVerbs(void)" (?erVerbs@@YAXXZ) already defined in Conjugator.obj
1>ErVerbs.obj : error LNK2005: "void __cdecl erVerbs(void)" (?erVerbs@@$$FYAXXZ) already defined in Conjugator.obj
1>IrVerbs.obj : error LNK2005: "void __cdecl irVerbs(void)" (?irVerbs@@YAXXZ) already defined in Conjugator.obj
1>IrVerbs.obj : error LNK2005: "void __cdecl irVerbs(void)" (?irVerbs@@$$FYAXXZ) already defined in Conjugator.obj
1>ReVerbs.obj : error LNK2005: "void __cdecl reVerbs(void)" (?reVerbs@@YAXXZ) already defined in Conjugator.obj
1>ReVerbs.obj : error LNK2005: "void __cdecl reVerbs(void)" (?reVerbs@@$$FYAXXZ) already defined in Conjugator.obj
I'd be extremely grateful if someone could tell me how to save functions in separate source files and #include them in one source files and use their functions without re-definition errors.
回答1:
Dont:
#include "ErVerbs.cpp"
in Conjugator.cpp, this is what causing your linker errors. By including your cpp files like that you redefine this function again.
You should create ErVerbs.h file and put in it declaration for your function:
#if !defined(ER_VERBS_H)
#define(ER_VERBS_H)
void erVerbs();
#endif
and in Conjugator.cpp, include #include "ErVerbs.h", and the same for other your functions.
回答2:
You should never include *.cpp files. Delete following
#include "ErVerbs.cpp"
#include "IrVerbs.cpp"
#include "ReVerbs.cpp"
Create erVerbs.h with following content:
void erVerbs();
and include it in Conjugator.cpp
#include "ErVerbs.h"
回答3:
As you included modules
#include "ErVerbs.cpp"
#include "IrVerbs.cpp"
#include "ReVerbs.cpp"
in module Conjugator.cpp then all four modules contain definitions of the functions and the compiler says about this.
You have to declare the functions in some header file and include it in all modules that uses these functions while their definitions to keep in one module (or among several modules) that will not be included in any other module.
回答4:
You're #including
a .cpp
file from another .cpp file
, so the function definition will exist in two places.
What you probably want to do is create a Conjugator.h
header file, declaring (but not defining) the function, and include that instead.
Also look up header guards (or #pragma once
) while you're at it, if you're not aware of how to prevent multiple declarations in .h
files.
来源:https://stackoverflow.com/questions/21835257/re-definitions-of-functions-while-including-files-in-c-error-lnk2005