2012-04-19 1 views
-2

codeblocks 버전 10.05에 문제점이 있습니다. 나는 C++ 프로젝트를 만들고, 나는이 같은 프로그램을 작성 :코드 블록에서 링크가 작동하지 않습니다!? (정의되지 않은 참조 ...)

MAIN.CPP

#include <iostream> 
#include "vectorddd.hpp" 


using namespace std; 

int main() 
{ 

    vector3D<int> tesztinttomb; 
    tesztinttomb.saveout("igen.dat"); 
    return 0; 
} 

헤더 파일 (vectorddd.hpp) :

#ifndef VECTORDDD_HPP_INCLUDED 
#define VECTORDDD_HPP_INCLUDED 

#include <iostream> 

template <class T> 
class vector3D { 
    T *x; 
    T *y; 
    T *z; 
    int meret; 
public: 
    void saveout(char* FileName); 

    vector3D(int Meret=0) : x(new T[meret]), y(new T[Meret]), z(new T[Meret]), meret(Meret) {} 

    ~vector3D() { delete [] x; delete [] y; delete [] z; } 
}; 


#endif // VECTORDDD_HPP_INCLUDED 

구현 파일 (vectorddd.cpp를) :

#include "vectorddd.hpp" 

template <class T> 
void vector3D<T>::saveout(char* FileName) { 
    int i=0;// I know this is stupid... but the emphasis is on the linking problem 

} 

그리고 그냥 연결되지 않습니다. .cpp 파일을 확인하고 속성 -> 빌드 옵션에서 설정을 컴파일해야한다는 것을 알고 있습니다. 그리고 나는 어떤 문제를 찾을 수없는, 그러나 다만 항상 같은 쓰기된다

In function `main': 
undefined reference to `vector3D<int>::saveout(char*)' 
||=== Build finished: 1 errors, 0 warnings ===| 

을 그리고 내 .HPP 파일로 .CPP 파일 구현을 넣을 경우 올바르게 작동합니다. 그러나 이것이 코드 블럭이 작동하는 방식이 아닙니다.

+5

[템플릿 정의를 헤더에 가야한다] (HTTP : // WWW를 .parashift.com/C++ - faq/templates.html # faq-35.13), 이것은 CB와는 아무런 관련이 없습니다. – ildjarn

+0

http://stackoverflow.com/q/115703/1214731 자세한 내용은 여기를 참조하십시오. – tmpearce

답변

2

템플릿이 헤더 파일에 있어야합니다. 생각해보십시오. 템플릿이 cpp 파일에있는 경우 어떻게 인스턴스화 할 수 있습니까?

이를 넣어해야합니다

template <class T> 
void vector3D<T>::saveout(char* FileName) { 
    int i=0;// I know this is stupid... but the emphasis is on the linking problem 

} 

당신의 헤더 vectorddd.hpp 파일에

비슷한 SO 포스트 참조 : Storing C++ template function definitions in a .CPP file

+0

내 기능에는 팀원이 없습니다. – Marcell

+0

cpp에있는 경우 왜 인스턴스화 할 수 없습니까? 그것은 함께 링크되어 있고, 컴파일러가 보게되면, 함수가 호출되어 함수 구현에서 함수를 간단하게 나타냅니다. – Marcell

+0

나는 이것이 단지 이런 식으로 작동하지 않는다고 생각할 수 없다. – Marcell