0

I have three files (see code below), one class in testingMatrix.h, testingMatrix.cpp and one mainMatrix.cpp. I want to use the matrix in the file mainMatrix.cpp, but it doesn't seem to work, I keep getting an error (I want to combine more files later, but the error is the same for this example);

Undefined symbols for architecture x86_64: "Matrix::Matrix(int, int, double const&)", referenced from: _main in mainMatrix-bd8935.o "Matrix::printMatrix() const", referenced from: _main in mainMatrix-bd8935.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

When I write g++ testingMatrix.cpp, ./a.out, I get a correct print. However, when I write g++ mainMatrix.cpp, ./a.out, I get the error stated above. I've looked around in the forum but can't seem to find a solution for my particular issue.

The first file;

//mainMatrix.cpp
#include "testingMatrix.h"

int main(){
    const double value = 3.0;
    int rows = 3;
    int columns = 3;
    Matrix testing(rows, columns, value);
    testing.printMatrix();
}

The second file;

//testingMatrix.h
#include <iostream>
#include <vector>

class Matrix{
    private:
        int the_rows;
        int the_columns;
        std::vector<std::vector<double> > the_matrix;

    public:
        Matrix(int rows, int cols, const double& value);
        void printMatrix() const;
};

The third file;

//testingMatrix.cpp
#include "testingMatrix.h"
#include <vector>

Matrix::Matrix(int rows, int cols, const double& value){
    the_rows = rows;
    the_columns = cols;
    the_matrix.resize(the_rows);
    for (int i = 0; i < the_matrix.size(); i++){
        the_matrix.at(i).resize(the_columns, value);
    }
}

void Matrix::printMatrix() const{
    std::cout << "Matrix: " << std::endl;
    for (unsigned i = 0; i < the_rows; i++) {
        std::cout << "[";
        for (unsigned j = 0; j < the_columns; j++) {
            if (j==the_columns-1){
                std::cout << the_matrix.at(i).at(j);
            }
            else{
                 std::cout << the_matrix.at(i).at(j) << ", ";
            }
        }
        std::cout << "]" << std::endl;
    }
}

int main(){
    Matrix testing(3, 3, 3.0);
    testing.printMatrix();
}
Jason
  • 3,777
  • 14
  • 27
armara
  • 535
  • 3
  • 17
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – UnholySheep Sep 26 '19 at 22:00
  • 1
    You need to compile/link both .cpp files together. But also make sure there is only one main() function. – rustyx Sep 26 '19 at 22:05
  • @rustyx oh that was the problem, I had checked that solution but I didn't realize I could only have one main() function. thanks man! – armara Sep 26 '19 at 22:08
  • 1
    Note the **warnings**, `testingMatrix.cpp:9, 16, 18 & 19 - "warning: comparison between signed and unsigned integer expressions [-Wsign-compare]"` Always compile with `-Wall -Wextra -pedantic` for gcc/clang or `/W3` for VS (`cl.exe`) and do not accept code until it compiles without warning. Example `g++ -Wall -Wextra -pedantic -Wshadow -std=c++11 -Ofast testingMatrix.cpp -o mainMatrix mainMatrix.cpp` – David C. Rankin Sep 26 '19 at 22:21
  • @DavidC.Rankin thanks for the tip, I'll be sure to use that. – armara Sep 26 '19 at 22:46
  • @armara I find `-Wmissing-declarations -Wredundant-decls` to be incredibly useful at enforcing sanity (every external function pre-declared exactly once, in the appropriate header, otherwise use `static` or something) – o11c Sep 26 '19 at 23:15

1 Answers1

0

You need to compile and link your sources...

g++ -o mainMatrix testMatrix.cpp mainMatrix.cpp

or

g++ -c testMatrix.cpp
g++ -c mainMatrix.cpp
g++ -o mainMatrix testMatrix.o mainMatrix.o
Jason
  • 3,777
  • 14
  • 27