gpt4 book ai didi

c++ - 未定义对 vector 的引用 g++ 链接器错误

转载 作者:太空宇宙 更新时间:2023-11-04 11:26:00 30 4
gpt4 key购买 nike

<分区>

我已经编译了我的源文件 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&)'

你能帮我解决这个问题吗

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com