为什么我的班级没有链接?

OHL*_*ÁLÁ 0 c++ compiler-errors g++

本周我开始将我的知识从C升级到C++,我想重载一些运算符

我有一个名为Matrix的课程

#include "lcomatrix.h"

inline Matrix::Matrix(unsigned rows, unsigned cols) :
        rows_(rows), cols_(cols)
{
        data_ = new double[rows * cols];
}

inline Matrix::~Matrix() {
    delete[] data_;
}

inline double& Matrix::operator()(unsigned row, unsigned col) {
    return data_[cols_ * row + col];
}

inline double Matrix::operator()(unsigned row, unsigned col) const {
    return data_[cols_ * row + col];
}
Run Code Online (Sandbox Code Playgroud)

内容lcomatrix.h

#include <iostream>

class Matrix {
public:
    Matrix(unsigned rows, unsigned cols);
    double& operator()(unsigned row, unsigned col);
    double operator()(unsigned row, unsigned col) const;

    ~Matrix(); // Destructor        
    Matrix& operator=(Matrix const& m); // Assignment operator      

private:
    unsigned rows_, cols_;
    double* data_;
};
Run Code Online (Sandbox Code Playgroud)

Main.cpp的

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


/*-
 * Application entry point.
 */
int main(void) {

    Matrix mx(12,12);

    //std::cout << mx << std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

制作文件:

CPPFLAGS=-I /path/lcomatrix/
EFLAGS=

all : main.o lcomatrix.o
    g++ $(EFLAGS) -o main.out  main.o lcomatrix.o 

main.o: lcomatrix.o
    g++ $(EFLAGS) $(CPPFLAGS) -c main.cpp

lcomatrix.o: 
    g++ $(EFLAGS) -c /home/robu/UbuntuOne/ChibiOS-RPi/lcomatrix/lcomatrix.cpp

clean:
    rm *.o main.out 
Run Code Online (Sandbox Code Playgroud)

当我尝试构建时,我收到以下链接错误:

make all 
g++  -c /home/robu/UbuntuOne/ChibiOS-RPi/lcomatrix/lcomatrix.cpp
g++  -I /home/robu/UbuntuOne/ChibiOS-RPi/lcomatrix/ -c main.cpp
g++  -o main.out  main.o lcomatrix.o 
main.o: In function `main':
main.cpp:(.text+0x1b): undefined reference to `Matrix::Matrix(unsigned int, unsigned int)'
main.cpp:(.text+0x2c): undefined reference to `Matrix::~Matrix()'
collect2: error: ld returned 1 exit status
make: *** [all] Error 1
Run Code Online (Sandbox Code Playgroud)

我想这是一个非常愚蠢的错误,但作为一个初学者,我无法弄清楚解决方案.

moo*_*dow 5

你的方法定义都是inline.为了内联函数,编译器需要在编译使用它的代码时查看其定义.

将函数定义放在可以在使用点看到的位置 - 在标题中,或者在另一个文件#included by Main.cpp中 - 或者不将它们标记为内联.