0

This week I started to upgrade my knowledge from C to C++, I would like to overload some operators

I have a class called 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];
}

The content of lcomatrix.h is

#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_;
};

Main.cpp

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


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

    Matrix mx(12,12);

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

    return 0;
}

Make file:

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 

When I try to build I receive the following link error:

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

I guess this a really stupid error, but as a beginner I couldn't figure out the solution.

Dabbler
  • 9,733
  • 5
  • 41
  • 64
Mokus
  • 10,174
  • 18
  • 80
  • 122
  • 5
    There is no "upgrade". You either learn proper, real C++, or you'll forever be doomed to write horrible C-style monstrosities. – Kerrek SB Nov 20 '12 at 21:36
  • 3
    replace `double* data_;` with `std::vector data_`, `data_ = new double[rows * cols];` with `data_.resize(0, rows * cols);`, and you'll no longer need `delete [] data_` – andre Nov 20 '12 at 21:37
  • According to this topic the vector is slower than the array http://stackoverflow.com/questions/3664272/stdvector-is-so-much-slower-than-plain-arrays – Mokus Nov 20 '12 at 21:51
  • `This week I started to upgrade my knowledge from c to c++, I would like to overloading some operators` I love this quote. – Lightness Races in Orbit Nov 23 '12 at 14:21
  • iUngi: Did you look at any of the answers, which all show that vector is not significantly slower than an array? – Mooing Duck Nov 24 '12 at 09:47

1 Answers1

5

Your method definitions are all inline. In order to inline a function, the compiler needs to see its definition whenever it is compiling the code that uses it.

Either put the function definitions somewhere they can be seen at the point of use - in the header, or in another file #included by Main.cpp - or don't mark them as inline.

moonshadow
  • 86,889
  • 7
  • 82
  • 122