こんにちは私は3つのファイルmain.cpp、My_Stack.cpp、My_Stack.hppをコンパイルしようとするとC++にいくつかの問題があります。私はヘッダーファイルを含めていないかのように、 "#include" My_Steak.hpp ""の代わりに "#include" My_Stack.cpp ""を追加するかのように、 "x86_64アーキテクチャの未定義シンボル"エラーがmain.cppで機能します良い。定義はMy_Stack.cppにヘッダファイルすなわち定義を対応する内側行かなければならないアーキテクチャの未定義シンボルx86_64 C++でエラーが発生しました。ヘッダがリンクされていません。
//main.cpp
#include <iostream>
#include "My_Stack.hpp"
int main(int argc, const char * argv[]) {
My_Stack<int> s = My_Stack<int>();
s.push(1);
s.push(2);
cout << s.pop() << endl;
cout << s.pop() << endl;
}
//My_Stack.hpp
#ifndef My_Stack_hpp
#define My_Stack_hpp
#include <stdio.h>
#include <string>
using namespace std;
template<class T>
class My_Stack {
public:
My_Stack();
void push(T v);
T pop();
private:
class My_Stack_Node {
public:
T data;
My_Stack_Node* next;
My_Stack_Node(T n) {
data = n;
next = NULL;
}
};
My_Stack_Node* head;
};
#endif /* My_Stack_hpp */
//My_Stack.cpp
#include "My_Stack.hpp"
#include <string>
#include <sstream>
using namespace std;
template<class T>
My_Stack<T>::My_Stack() {
this->head = NULL;
}
template<class T>
void My_Stack<T>::push(T v) {
if (this->head == NULL) {
this->head = new My_Stack_Node(v);
}
else {
My_Stack_Node* aux = new My_Stack_Node(v);
aux->next = this->head;
this->head = aux;
}
}
template<class T>
T My_Stack<T>::pop() {
My_Stack_Node* aux = this->head;
this->head = this->head->next;
return aux->data;
}