缺少符号错误(未定义的引用)

发布于 2024-11-03 21:11:19 字数 2155 浏览 7 评论 0原文

我试图用 DS.h 中的声明和 DS.cpp 中的实现来定义类 DS 代码非常小,所以这里是清单:

 /*
  * DS.h
  */

 #ifndef DS_H_
 #define DS_H_

 #include "Node.h"

 template<class T>
 class DS
 {
     public:
         static const int BST;
         static const int SLL;
         static const int DLL;

         DS(const int);
         ~DS();

     private:
         int m_type;
         Node<T> *head;
 };

 #endif /* DS_H_ */

并且,

 /*
  * DS.cpp
  */

 #include "DS.h"

 template<class T> const int DS<T>::BST = 0;
 template<class T> const int DS<T>::SLL = 1;
 template<class T> const int DS<T>::DLL = 2;

 template<class T>
 DS<T>::DS(const int type) :
     m_type(type), head(0)
 {
 }

 template<class T>
 DS<T>::~DS()
 {
 }

主程序是:

 #include "DS.h"

 int main()
 {
     DS<int> *sll1 = new DS<int> (DS<int>::SLL);
     delete sll1;
     return 0;
 }

当我尝试编译这个程序时,我收到以下错误:

 g++ -O2 -g -Wall -Wextra -Weffc++ -fmessage-length=0   -c -o Node.o Node.cpp
 g++ -O2 -g -Wall -Wextra -Weffc++ -fmessage-length=0   -c -o DS.o DS.cpp
 g++ -O2 -g -Wall -Wextra -Weffc++ -fmessage-length=0   -c -o main.o main.cpp
 DS.h: In instantiation of ?DS<int>?:
 main.cpp:13:   instantiated from here
 DS.h:15: warning: ?class DS<int>? has pointer data members
 DS.h:15: warning:   but does not override ?DS<int>(const DS<int>&)?
 DS.h:15: warning:   or ?operator=(const DS<int>&)?
 g++ -o ds.exe Node.o DS.o main.o 
 main.o: In function `main':
 /cygdrive/c/Documents and Settings/ansrivastava/My Documents/src/ds/main.cpp:13: undefined reference to `DS<int>::SLL'
 /cygdrive/c/Documents and Settings/ansrivastava/My Documents/src/ds/main.cpp:13: undefined reference to `DS<int>::DS(int)'
 /cygdrive/c/Documents and Settings/ansrivastava/My Documents/src/ds/main.cpp:14: undefined reference to `DS<int>::~DS()'
 collect2: ld returned 1 exit status
 make: *** [ds.exe] Error 1

现在,如果我从 DS.cpp 中删除所有代码并将其粘贴到 DS 中。 h,一切编译正常。知道我做错了什么吗?

I have trying to define a class DS with the declaration in DS.h and implementation at DS.cpp
The code is very small so here is the listing:

 /*
  * DS.h
  */

 #ifndef DS_H_
 #define DS_H_

 #include "Node.h"

 template<class T>
 class DS
 {
     public:
         static const int BST;
         static const int SLL;
         static const int DLL;

         DS(const int);
         ~DS();

     private:
         int m_type;
         Node<T> *head;
 };

 #endif /* DS_H_ */

And,

 /*
  * DS.cpp
  */

 #include "DS.h"

 template<class T> const int DS<T>::BST = 0;
 template<class T> const int DS<T>::SLL = 1;
 template<class T> const int DS<T>::DLL = 2;

 template<class T>
 DS<T>::DS(const int type) :
     m_type(type), head(0)
 {
 }

 template<class T>
 DS<T>::~DS()
 {
 }

The main program is:

 #include "DS.h"

 int main()
 {
     DS<int> *sll1 = new DS<int> (DS<int>::SLL);
     delete sll1;
     return 0;
 }

When I try to compile this program, I get the following error:

 g++ -O2 -g -Wall -Wextra -Weffc++ -fmessage-length=0   -c -o Node.o Node.cpp
 g++ -O2 -g -Wall -Wextra -Weffc++ -fmessage-length=0   -c -o DS.o DS.cpp
 g++ -O2 -g -Wall -Wextra -Weffc++ -fmessage-length=0   -c -o main.o main.cpp
 DS.h: In instantiation of ?DS<int>?:
 main.cpp:13:   instantiated from here
 DS.h:15: warning: ?class DS<int>? has pointer data members
 DS.h:15: warning:   but does not override ?DS<int>(const DS<int>&)?
 DS.h:15: warning:   or ?operator=(const DS<int>&)?
 g++ -o ds.exe Node.o DS.o main.o 
 main.o: In function `main':
 /cygdrive/c/Documents and Settings/ansrivastava/My Documents/src/ds/main.cpp:13: undefined reference to `DS<int>::SLL'
 /cygdrive/c/Documents and Settings/ansrivastava/My Documents/src/ds/main.cpp:13: undefined reference to `DS<int>::DS(int)'
 /cygdrive/c/Documents and Settings/ansrivastava/My Documents/src/ds/main.cpp:14: undefined reference to `DS<int>::~DS()'
 collect2: ld returned 1 exit status
 make: *** [ds.exe] Error 1

Now, if I remove all the code from DS.cpp and paste it into DS.h, everything compiles fine. Any idea what am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

夏尔 2024-11-10 21:11:19

现在,如果我从 DS.cpp 中删除所有代码并将其粘贴到 DS.h 中,则一切都会正常编译。知道我做错了什么吗?

请参阅此条目 在 C++ FAQ 中有关单独编译的内容。

Now, if I remove all the code from DS.cpp and paste it into DS.h, everything compiles fine. Any idea what am I doing wrong?

See this entry in the C++ FAQ about separate compilation.

荆棘i 2024-11-10 21:11:19

static const 值成员必须立即初始化,就像任何 const 值一样。所以:

     static const int BST = 0;
     static const int SLL = 1;
     static const int DLL = 2; 

A static const value member must be initialized immediately, as any const value. So:

     static const int BST = 0;
     static const int SLL = 1;
     static const int DLL = 2; 
山川志 2024-11-10 21:11:19

您自己说,如果您将代码从 DS.cpp 移至 DS.h,它可以很好地编译并询问您做错了什么。答案就是,您的 .cpp 文件中有代码。当 DS.cpp 编译时,它不会定义 DS <整数>由于这是在主文件中完成的,因此需要包含 DS.h 以便

const int DS<int>::BST = 0;
const int DS<int>::SLL = 1;
const int DS<int>::DLL = 2;

进行编译。不要忘记 DS 只是一个模板。编译 DS.cpp 没有任何意义,因为它只包含模板。

You are saying yourself that it compiles fine if you move the code from DS.cpp to DS.h and asking what you are doing wrong. The answer is just that, you have the code in the .cpp file. When DS.cpp is compiled it will not define DS < int > since this is done in your main file, therefore DS.h needs to be included so

const int DS<int>::BST = 0;
const int DS<int>::SLL = 1;
const int DS<int>::DLL = 2;

will be compiled. Dont forget that DS is just a template. It means nothing to compile DS.cpp since it only contains the template.

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