【问题标题】:Linking not successful upon compiling a dummy C++ program编译虚拟 C++ 程序时链接不成功
【发布时间】:2013-09-06 08:17:56
【问题描述】:

我有这个文件:myfuncts.hpp:

#ifndef FUNCTS_
#define FUNCTS_
namespace root {
namespace functs {
template <typename T>
class f1 {
public:
  T r;
  f1();
};
}
}
#endif

我有实现:myfuncts.cpp:

#include "myfuncts.hpp"
template <typename T>
root::functs::f1<T>::f1() { /* do somethng */ }

然后我有我的主要程序:

#include "impulses.hpp"
int main(int argc, char** argv);
int main(it argc, char** argv) {
  root::functs::f1<double> f();
}

我编译它:

g++ main.cpp functs.cpp

得到了这个:

/tmp/ccrdJEQt.o: 在函数main': main.cpp:(.text+0x53): undefined reference toroot::functs::f1::f1()' collect2: ld 返回 1 退出状态

我做错了什么?

【问题讨论】:

    标签: c++ linker g++


    【解决方案1】:

    您可能想了解the most vexing parse,因为当您这样做时

    root::functs::f1<double> f();
    

    您将f 声明为一个返回root::functs::f1&lt;double&gt; 对象的函数。

    您可能还想阅读this old question,了解您实际收到undefined reference 错误的原因。这是因为头文件没有完全定义类。对于模板类,完整的实现也必须在头文件中。

    【讨论】:

    • 我怎么这么笨……你说得对!!!我记得我的上帝...非常感谢,当然标题必须去那里...
    猜你喜欢
    • 1970-01-01
    • 2022-01-11
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    相关资源
    最近更新 更多