2013-02-21 2 views
0

몇 년 후에 C++을 선택했습니다. 지금은 다른 클래스에서 사용할 간단한 행렬 클래스를 구현하려고합니다. GManNickG's lead에 따라, 여기 ("SimpleMatrix.h"선언) 내 SimpleMatrix입니다 :Xcode 링커에서 특정 메서드를 볼 수없는 이유를 이해할 수 없습니다.

#pragma once 
#include <vector> 

template <typename T> 
class SimpleMatrix {  
    unsigned int numCols, numRows;  
public:  
    std::vector<T> data; 

    SimpleMatrix(unsigned int cols, unsigned int rows) : 
    numCols(cols), 
    numRows(rows), 
    data(numCols * numRows) 
    {}; 

    T getElement(unsigned int column, unsigned int row); 
    void setShape(unsigned int column, unsigned int row, const T& initValue);  
}; 

하고 ("SimpleMatrix.cpp"에서)로 구현 :

#include "SimpleMatrix.h" 

template <class T> 
T SimpleMatrix<T>::getElement(unsigned int column, unsigned int row) { 
    return data[row * numCols - 1];  
} 

template <class T> 
void SimpleMatrix<T>::setShape(unsigned int columns, unsigned int rows, const T& initValue) { 
    numCols = columns; 
    numRows = rows; 
    data.assign(columns * rows, initValue); 
} 

을 지금, 나는이 main에서 SimpleMatrix를 사용할 때 , 그것은 컴파일, 링크 및 잘 작동합니다.

#include "SimpleMatrix.h" 

class Container { 
public:  
    SimpleMatrix<int> matrix;  
    Container();  
    void doStuff(); 
}; 

하고 ("Container.cpp"에서)로 구현 :

#include "Container.h" 
#include "SimpleMatrix.h" 

void Container::doStuff() {  
    this->matrix.setShape(2, 2, 0); 
    this->matrix.getElement(1, 1); 
} 

엑스 코드는 불평 나는 ("Container.h"에서)로 선언 된 객체 Container에서 사용하려고하면

Undefined symbols for architecture x86_64:

"SimpleMatrix<int>::getElement(unsigned int, unsigned int)", referenced from: 
Container::doStuff() in Container.o 

"SimpleMatrix<int>::setShape(unsigned int, unsigned int, int const&)", referenced from: 
Container::doStuff() in Container.o 

ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

본인은 "페이즈를 구축/컴파일 소스"-settings을 확인했습니다, 그리고 세 개의 파일이 거기 (MAIN.CPP, SimpleMatrix.cpp 및 Container.cpp).

이 코드에는 여러 가지 문제가있을 수 있습니다. 마음에 떠오르는 것은 SimpleMatrix에 대한 기본 생성자가 없다는 것입니다.하지만 실제로 여기에 관심이있는 것은 아닙니다. 두 가지 경우의 근본적인 차이점을 이해할 수 없습니다.

도움을 주시면 대단히 감사하겠습니다.

+0

감사합니다. Andy, 그게 다야! – conciliator

답변

2

템플릿의 구현은 헤더 파일에 있어야합니다.

관련 문제