0

我已经编译了我的源文件 mycpp.c 的依赖文件,并按照它们使用的顺序链接了这些文件。所有依赖文件都在同一个文件夹中。

   **//Compilation**
     $ g++  -c -w -Wno-deprecated String.c Vector.c DPStream.c CJ_Base.c mycpp.c
     **//Linking**
     $ g++ -g -o myexe String.o Vector.o DPStream.o CJ_Base.o mycpp.o
Vector.h
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

#include "String.h"
#include "Storage.h"


template <class Object>
class Vector : public Storage{

        public:
                // ctors & dtor
                Vector ();
                Vector (int);
                ~Vector();

                // overloaded operators
                Object& operator[] (int nSubscript);
                void operator<<(Object &);
                void operator<<(char *);

                // access methods
                int  Count(){return _iCurrElem;};
                int  Print(ofstream &);
                void Flush();
                Resize(int);

                int  IsType() {return VectorType;};

        private:
                long _iCurrElem;
                Object *moj;
                long _iUpperBound;

                void _reserve(int);
};
#endif


Vector.c
---------
#include"Vector.h"

template <class Object>
Vector<Object>::Vector ()
{
   moj = new Object[3];
}

template <class Object>
Vector<Object>::Vector (int e)
{
moj = new Object[e];
}

template <class Object>
Vector<Object>::~Vector ()
{
  delete[] moj;
} 

template <class Object>
Vector<Object>::operator<<(Object &)
{
//stmt
}

template <class Object>
Vector<Object>::operator<<(char* ch)
{
//stmt
}

template <class Object>
Vector<Object>::Print(ofstream &foutput)
{
//stmt
}

在 mycpp.h 的以下顺序中包含头文件

#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include "String.h"
#include "Vector.h"
#include "DPStream.h"
#include "CJ_Base.h"

但是 g++ 编译器仍然抛出错误 粘贴在链接错误下方

        DPStream.o: In function `DPStream::operator<<(Vector<String>&)':
    DPStream.c:(.text+0x396): undefined reference to `Vector<String>::Print(std::basic_ofstream<char, std::char_traits<char> >&)'
    mycpp.o: In function `mycpp::ProcessFile()':
    mycpp.o:(.text+0x24e): undefined reference to `Vector<String>::operator<<(String&)'
    mycpp.o:(.text+0x2e5): undefined reference to `Vector<String>::operator<<(char*)'
    mycpp.o:(.text+0x310): undefined reference to `Vector<String>::Flush()'
    mycpp.o:(.text+0x32b): undefined reference to `Vector<String>::operator<<(String&)'
    mycpp.o:(.text+0x346): undefined reference to `Vector<String>::operator<<(String&
    mycpp.o: In function `mycpp::~mycpp()':
    mycpp.o:(.text+0x24e): undefined reference to `Vector<String>::operator<<(String&)'

你能帮我解决这个问题吗

4

2 回答 2

1

您没有向我们展示Vector定义的位置和方式,而只是猜测:您正在编译一个Vector.c. 如果这个文件包含模板的实现,那么你做错了;当模板被实例化时,模板的实现必须是可见的。您可以将它放在头文件中,也可以包含头文件中的源代码,但您不编译源代码。

此外,大多数编译器认为 a.c是 C 源代码,而不是 C++。您几乎可以肯定要将源的名称更改为.cpp.cc。当您使用它时,我也会更改纯 C++ 标头的名称。虽然无处不在,但对 C 头文件和纯 C++ 头文件使用相同的命名约定可能会造成混淆。

最后,<name.h>C 标准头文件的形式不标准;如果它们存在(它们通常不存在),它们应该引用旧的、预标准版本的库。(从您的错误消息来看,显然不是这种情况。)

于 2013-02-07T11:24:48.057 回答
0

您所有的错误都与模板函数有关。

请记住,在定义模板时,您必须将定义放在 header中,而不是放在.cpp文件中。其他模块需要定义来执行它们自己的模板实例化。

所以一个快速的解决方法是#include "Vector.c"Vector.h.

或者将整个模板实现放在.h文件中。


(也请.cpp不要.c用于 C++ 源文件;hpp用于 C++ 头文件也很好)

于 2013-02-07T11:23:34.597 回答