gpt4 book ai didi

c++ - typedef 和模板的 undefined symbol ?

转载 作者:太空狗 更新时间:2023-10-29 20:45:58 25 4
gpt4 key购买 nike

<分区>

这看起来很简单,但我不知道哪里出了问题。我正在实现 C++ vector 类(仅适用于 int,不适用于模板),带有迭代器模板或 typedef 的函数在编译时出现以下错误:

Undefined symbols:
"void vectorInt::assign<int>(int, int)", referenced from:
_main in ccNVdR23.o
"void vectorInt::assign<int*>(int*, int*)", referenced from:
_main in ccNVdR23.o
_main in ccNVdR23.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

源文件的重要部分是:

vectorInt.h

#include <cstdlib>
#include <stdexcept>

typedef unsigned int size_type;

class vectorInt {
private:
int* array;
size_type current_size;
size_type current_capacity;
public:
.
.
.
template <class InputIterator>
void assign(InputIterator first, InputIterator last);
void assign(size_type n, const int u);
};

#endif // VECTORINT_H

vectorInt.cpp

#include vectorInt.h
.
.
.
template <class InputIterator>
void vectorInt::assign(InputIterator first, InputIterator last) {
clear();
InputIterator it = first;
int count = 0;
while(it++ != last) {
count++;
}

reserve(count);
while(first != last) {
this->push_back(*first++);
}
}

void vectorInt::assign(size_type n, const int u) {
clear();
reserve(n);

for(int i=0; i<(int)n; i++)
push_back(u);
}

主要.cpp

#include <cstdlib>
#include <stdexcept>
#include <iostream>
#include "vectorInt.h"

using namespace std;

int main(int argc, char** argv) {
vectorInt first;
vectorInt second;
vectorInt third;

first.assign(7, 100);

vectorInt::iterator it;
it = first.begin()+1;
second.assign(it, first.end()-1); // the 5 central values of first

int myints[] = {1776,7,4};
third.assign(myints, myints+3); // assigning from array.

return 0;
}

仅供引用:我知道主要方法使用 vectorInt::iterator,但这不是问题所在,因此我没有将它包含在源代码中。

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