C++无法从 main.cpp 中的另一个文件实例化类

发布于 2024-11-05 23:17:38 字数 1419 浏览 0 评论 0原文

我无法让它工作

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不可一世的女人 2024-11-12 23:17:38
#include "addr.cpp"

您将 .cpp 文件包含到 main.cpp 中,这就是导致问题的原因。您应该包含头文件,如下所示:

#include "addr.h"  //add this instead, to your main.cpp

因为它的头文件包含类 Foo定义

#include "addr.cpp"

You're including the .cpp file to your main.cpp, which is what causing the problem. You should include the header file instead, as:

#include "addr.h"  //add this instead, to your main.cpp

Because its the header file which is containing the definition of the class Foo.

青芜 2024-11-12 23:17:38

在main.cpp中

<前><代码>#include“addr.cpp”

应该是

#include "addr.h"

如果您#include“addr.cpp”,您可以编译main.cpp(甚至将其链接到自己的可执行文件中),但是当您链接< code>main.obj 和 add.obj,您会收到重复定义错误,因为类 Foo 现在已在两个对象文件中完全定义。

In main.cpp

#include "addr.cpp"

should be

#include "addr.h"

If you #include "addr.cpp", you can compile main.cpp (and even link it into an executable on it's own), but when you link main.obj and add.obj, you get a duplicate definition error, because class Foo is now fully defined in both object files.

红焚 2024-11-12 23:17:38

在 main.cpp 中,您应该包含头文件,而不是 cpp 文件。

更改

#include "addr.cpp"

#include "addr.h"

In main.cpp, you should include the header file, rather than the cpp file.

Change

#include "addr.cpp"

To

#include "addr.h"
御守 2024-11-12 23:17:38

您可以#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.

十秒萌定你 2024-11-12 23:17:38

有两种方法将类数据放入 main.cpp

1) 在main.cpp 文件中包含头文件“addr.h”,这样现在您有了类和成员函数的定义,但没有它们的实现部分。因此,在编译时,代码应该是:
g++ addr.cpp main.cpp.

2) 在 main.cpp 中包含 cpp 文件“addr.cpp”,这样您就可以自动获得定义,因为“addr.h”包含在“addr.cpp”中。现在您必须仅编译 main.cpp 文件:g++ main.cpp

第一种方法是标准的,是维护模块化和封装性的良好编程实践......

There are two ways of getting the class data into main.cpp

1) include the header file "addr.h" in the main.cpp file, so now you have the class and member functions' definitions, but don't have the implementation part of them. So while compiling, the code should be :
g++ addr.cpp main.cpp.

2) include the cpp file "addr.cpp" in the main.cpp, so you automatically have the definitions also as "addr.h" is included in "addr.cpp". Now you must compile only the main.cpp file: g++ main.cpp.

First method is standard and is a good programming practice for maintaining modularity and encapsulation...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文