C ++ - 对所有类的所有方法的未定义引用

时间:2017-05-22 07:09:30

标签: c++ class

我得到“未定义的引用...”错误。虽然,我在代码中找不到任何错误。

  

main.cpp:11:未定义引用`Grid :: Grid(int,int)'

对于2个类的所有方法都会发生这种情况,所以我将在这里写一个类,随意请求另一个类(另一个是一个很长的代码)。

main.cpp中:

#include <iostream>
#include "Grid.h"

using namespace std;

int main() {
    Grid<int> a(5, 5);

    return 0;
}

Grid.h:

#ifndef GRID_H
#define GRID_H

#include <vector>

using namespace std;

template <class T>
class Grid{
private:
    int W, H;
    vector<vector<T>> grid;
    Grid();
public:
    Grid(int w, int h);

    bool SetContent(int x, int y, T content);
    T GetContent(int x, int y);
};

#endif /* GRID_H */

Grid.cpp:

#include "Grid.h"
#include <vector>

using namespace std;

template <class T> 
Grid<T>::Grid(){}

template <class T>
Grid<T>::Grid(int w, int h){
    W = w;
    H = h;
    grid.resize(h);
    for(int y = 0; y < h; y++)
        grid[y].resize(w);
}

template <class T>
bool Grid<T>::SetContent(int x, int y, T content){
    if(x > 0 && x < W && y > 0 && y < H){
        grid[x][y] = content;
        return true;
    }
    return false;
}

template <class T>
T Grid<T>::GetContent(int x, int y){
    if(x > 0 && x < W && y > 0 && y < H){
        return grid[x][y];
    }
    return grid[x][y];
}

如果需要,这是编译器命令(我猜这是编译错误):

  

g ++ -o dist / Debug / MinGW-Windows / pacman build / Debug / MinGW-Windows / Grid.o build / Debug / MinGW-Windows / Vector2.o build / Debug / MinGW-Windows / WinAPI_Console.o build / Debug / MinGW-Windows / main.o -static-libgcc -static-libstdc ++

如果您需要更多信息,请问,我会在这里等待回复。

0 个答案:

没有答案