これはとても簡単に思えますが、何が問題なのかわかりません。C++ ベクトル クラス (テンプレートではなく 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);
}
main.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;
}
参考までに: main メソッドが vectorInt::iterator を使用していることは知っていますが、それは問題ではないため、ソース コードには含めませんでした。