Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 试图同时编译三个template.cpp文件会导致;错误:重新定义“方法”;_C++_Linux_Templates_Header_Linker - Fatal编程技术网

C++ 试图同时编译三个template.cpp文件会导致;错误:重新定义“方法”;

C++ 试图同时编译三个template.cpp文件会导致;错误:重新定义“方法”;,c++,linux,templates,header,linker,C++,Linux,Templates,Header,Linker,我有一个大学项目,我设法使用自己的makefile/terminal命令来完成。但我在大学的自动阅卷机上得零分。我认为这是因为他们使用一个makefile,它试图与编译器在一行中执行所有三个cpp文件(如果可能的话),这很难判断,因为标记不会给出任何反馈。当我尝试编译所有三个.cpp文件(g++main.cpp A.cpp B.cpp)时,会收到以下错误消息: B.cpp:4:3:错误:重新定义'tb::getVal()' tb::getVal(){ A.cpp:4:6:错误:重新定义“作废A

我有一个大学项目,我设法使用自己的makefile/terminal命令来完成。但我在大学的自动阅卷机上得零分。我认为这是因为他们使用一个makefile,它试图与编译器在一行中执行所有三个cpp文件(如果可能的话),这很难判断,因为标记不会给出任何反馈。当我尝试编译所有三个.cpp文件(g++main.cpp A.cpp B.cpp)时,会收到以下错误消息:

  • B.cpp:4:3:错误:重新定义'tb::getVal()' tb::getVal(){
  • A.cpp:4:6:错误:重新定义“作废A::打印(B)” 无效A::打印(B B B){
在我的MRE中,以下需要保持真实:

  • A和B必须有.h和.cpp文件
  • main.cpp只能包括“A.h”
  • A必须要求B才能工作
这是我的MRE: -它生成一个实例B,然后使用实例A打印一条值为实例B的消息

main.cpp 它应该使用的命令(如果可能) 您永远不应该在头中包含“.cpp”。函数模板的实现必须在头中,而不是在
cpp
中。在您的示例中,
A.cpp
B.cpp
根本不需要。如果任务仍然需要它们,请将它们设置为空,除了
\include
指令

编辑:如果你的汽车销售商不喜欢它,你也可以尝试内联你的实现,例如

template <class T>
class B{
public:
  T val;
  T getVal(){
     return val;
  }
};
模板
B类{
公众:
T值;
T getVal(){
返回val;
}
};

您现在的问题是,当您尝试编译B.cpp时,以下是:

#include "B.h"

template <class T>
T B<T>::getVal(){
  return val;
}
#包括“B.h”
模板
tb::getVal(){
返回val;
}
这是:

#ifndef BBBB
#define BBBB

template <class T>
class B{
public:
  T val;
  T getVal();
};

#include "B.cpp"

#endif
#ifndef BBBB
#定义BBBB
模板
B类{
公众:
T值;
T getVal();
};
#包括“B.cpp”
#恩迪夫
结合到以下内容中:

#ifndef BBBB
#define BBBB

template <class T>
class B{
public:
  T val;
  T getVal();
};

#ifndef BBBB     // <- BBBB has been defined at this point, so everything between  this and the #endif is skipped
#endif

template <class T>
T B<T>::getVal(){
  return val;
}

#endif

template <class T>
T B<T>::getVal(){
  return val;
}
#ifndef BBBB
#定义BBBB
模板
B类{
公众:
T值;
T getVal();
};

#ifndef BBBB//我在这里还是新来的,我意识到我用了太多的词来描述我的问题。我试图减少它,但如果这仍然太多,请让我知道。不要包括cpp文件。当你将实现放在头文件而不是.cpp文件中时,automarker会检测到,因此我仍然会得到零,但谢谢。你确定它会被删除吗即使对于模板实现也是如此?如果是的话,我会说这是标记中的一个错误。是的,我确定。我认为有一种方法可以做到这一点,但很难知道,因为我们不知道makefile看起来像什么,或者它们运行的测试代码是什么,甚至不知道编译器会给出什么错误。我同意#包含.cpp文件是一种不好的做法。我说了对于主题管理员,在这种特殊情况下,我们应该包括.cpp文件。我会和他们谈谈,看看他们怎么说。非常感谢您的时间!
#include "B.h"

template <class T>
T B<T>::getVal(){
  return val;
}
g++ main.cpp

./a.out
g++ main.cpp A.cpp B.cpp

./a.out
template <class T>
class B{
public:
  T val;
  T getVal(){
     return val;
  }
};
#include "B.h"

template <class T>
T B<T>::getVal(){
  return val;
}
#ifndef BBBB
#define BBBB

template <class T>
class B{
public:
  T val;
  T getVal();
};

#include "B.cpp"

#endif
#ifndef BBBB
#define BBBB

template <class T>
class B{
public:
  T val;
  T getVal();
};

#ifndef BBBB     // <- BBBB has been defined at this point, so everything between  this and the #endif is skipped
#endif

template <class T>
T B<T>::getVal(){
  return val;
}

#endif

template <class T>
T B<T>::getVal(){
  return val;
}