C++无法从 main.cpp 中的另一个文件实例化类
我无法让它工作
Addr.h
#ifndef ADDR_H
#define ADDR_H
class Foo{
public:
Foo(); // function called the default constructor
Foo( int a, int b ); // function called the overloaded constructor
int Manipulate( int g, int h );
private:
int x;
int y;
};
#endif
Addr.cpp
#include "addr.h"
Foo::Foo(){
x = 5;
y = 10;
}
Foo::Foo( int a, int b ){
x = a;
y = b;
}
int Foo::Manipulate( int g, int h ){
return x = h + g*x;
}
main.cpp
#include "addr.cpp"
int main(){
Foo myTest = Foo( 20, 45 );
while(1){}
return 0;
}
我做错了什么?我收到这些链接器错误:
error LNK2005: "public: int __thiscall Foo::Manipulate(int,int)" (?Manipulate@Foo@@QAEHHH@Z) 已在 addr.obj c:\Users\christian\documents\ 中定义Visual Studio 2010\Projects\Console Test\Console Test\main.obj
错误 LNK2005:“public: __thiscall Foo::Foo(int,int)” (??0Foo@@QAE@HH@Z) 已在 addr.obj 中定义c:\Users\christian\documents\visual studio 2010\Projects\Console Test\Console Test\main.obj
错误 LNK2005: “public: __thiscall Foo::Foo(void)” (??0Foo@@QAE@XZ) 已经在 addr.obj c:\Users\christian\documents\visual studio 2010\Projects\Console Test\Console Test\main.obj 中定义
错误 LNK1169:找到一个或多个多重定义的符号 c:\users\christian\documents\visual studio 2010\Projects\Console Test\Release\Console Test.exe
我将不胜感激任何类型的帮助!
I cannot get this to work
Addr.h
#ifndef ADDR_H
#define ADDR_H
class Foo{
public:
Foo(); // function called the default constructor
Foo( int a, int b ); // function called the overloaded constructor
int Manipulate( int g, int h );
private:
int x;
int y;
};
#endif
Addr.cpp
#include "addr.h"
Foo::Foo(){
x = 5;
y = 10;
}
Foo::Foo( int a, int b ){
x = a;
y = b;
}
int Foo::Manipulate( int g, int h ){
return x = h + g*x;
}
main.cpp
#include "addr.cpp"
int main(){
Foo myTest = Foo( 20, 45 );
while(1){}
return 0;
}
What am i doing wrong? I get these linker errors:
error LNK2005: "public: int __thiscall Foo::Manipulate(int,int)" (?Manipulate@Foo@@QAEHHH@Z) already defined in addr.obj c:\Users\christian\documents\visual studio 2010\Projects\Console Test\Console Test\main.obj
error LNK2005: "public: __thiscall Foo::Foo(int,int)" (??0Foo@@QAE@HH@Z) already defined in addr.obj c:\Users\christian\documents\visual studio 2010\Projects\Console Test\Console Test\main.obj
error LNK2005: "public: __thiscall Foo::Foo(void)" (??0Foo@@QAE@XZ) already defined in addr.obj c:\Users\christian\documents\visual studio 2010\Projects\Console Test\Console Test\main.obj
error LNK1169: one or more multiply defined symbols found c:\users\christian\documents\visual studio 2010\Projects\Console Test\Release\Console Test.exe
I would appreciate any kind of help!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您将 .cpp 文件包含到
main.cpp
中,这就是导致问题的原因。您应该包含头文件,如下所示:因为它的头文件包含类
Foo
的定义。You're including the .cpp file to your
main.cpp
, which is what causing the problem. You should include the header file instead, as:Because its the header file which is containing the definition of the class
Foo
.在main.cpp中
应该是
如果您
#include“addr.cpp”
,您可以编译main.cpp
(甚至将其链接到自己的可执行文件中),但是当您链接< code>main.obj 和add.obj
,您会收到重复定义错误,因为类Foo
现在已在两个对象文件中完全定义。In
main.cpp
should be
If you
#include "addr.cpp"
, you can compilemain.cpp
(and even link it into an executable on it's own), but when you linkmain.obj
andadd.obj
, you get a duplicate definition error, because classFoo
is now fully defined in both object files.在 main.cpp 中,您应该包含头文件,而不是 cpp 文件。
更改
为
In main.cpp, you should include the header file, rather than the cpp file.
Change
To
您可以#include“addr.h”(如前所述),或使构建系统不编译addr.cpp。前者通常是首选,尽管后者也可以解决问题并且通常与自动代码生成工具的输出一起使用。
You can either #include "addr.h" (as already stated), or make the build system not compile addr.cpp. The former is usually preferred, though the latter would also solve the trouble and is often used with automatic code generation tools' output.
有两种方法将类数据放入 main.cpp
第一种方法是标准的,是维护模块化和封装性的良好编程实践......
There are two ways of getting the class data into main.cpp
First method is standard and is a good programming practice for maintaining modularity and encapsulation...