私はJavaコードをC++に変換しようとするC++ newbです。私は何か間違っていると思います。 Javaコードは以下の通りです:JavaをC++に変換する:テンプレートとジェネリックス
class Container<T> {
public T internal;
public static Container lastInstance;
Container(T value){
internal = value;
lastInstance = this;
}
}
class test2 {
public static void main(String argv[]){
Container<String> str = new Container<String>("moo");
Container<Integer> myint = new Container<Integer>(777);
System.out.println("Last Instance: " + str.lastInstance.internal);
}
}
そして、私はC++のために、これまでに以下のいる:
#include <iostream>
using namespace std;
template <class T> class Container {
public:
T internal;
static Container<T> *lastInstance;
Container(T val){
internal = val;
lastInstance = this;
};
};
int main(){
Container<int> *myint = new Container<int>(4);
Container<string> *str = new Container<string>("hello");
cout << myint->lastInstance->internal << endl;
cout << str->lastInstance->internal << endl;
return 0;
}
私がコンパイルしようとすると、私はUndefined symbols for architecture x86_64:
エラーを取得します。私は間違って何をしていますか?
編集:エラーの詳細。 エラーは本当にリンクエラーです。
Undefined symbols for architecture x86_64:
"Container<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::lastInstance", referenced from:
_main in template-d68cfa.o
Container<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::Container(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in template-d68cfa.o
"Container<int>::lastInstance", referenced from:
_main in template-d68cfa.o
Container<int>::Container(int) in template-d68cfa.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
EDIT2:
私が知っています。しかし、皆さんが助けてくれると思っていました。私が言ったように、私はC++ newbです。 – modsoussi
コンパイルエラーではなくリンクエラーのようです。この質問の詳細を指定する必要があります。あなたのコンパイルコマンドと使用しているOS /コンパイラを含めてください。 – Daniel
私はリンクエラーの本文を質問 – modsoussi