0

Im trying to execute my program, And the makefile returns an error about Duplicate Symbols. Not sure what's wrong.

Here are the files:

main.cpp

#include "assignment1.cpp"
#include <iostream>
using namespace std;

int main(){
  Matrix m1;
  Matrix m2;
  m1.toString();
  m2.toString();
  Matrix m3;
  m3 = m1*m2;
  if(m1==m2){
    cout<<"They are even"<<endl;
  } else{
    cout<<"They are not Even"<<endl;
  }



  return 0;
}

assignment1.h

#ifndef  MATRIX_H
#define MATRIX_H

class Matrix {
  public:
    int myArray[3][3];

  public:
    Matrix ();

    void toString();

    Matrix operator *(const Matrix& sec);

    bool operator ==(const Matrix& sec);



};
#endif

assignment1.cpp

#include "assignment1.h"
#include <iostream>
using namespace std;

Matrix::Matrix(){
    for(int i = 0; i < 3; i++)
    {
      for(int j = 0; j < 3; j++)
      {
        int randomnum = rand() % 100;
        cin >> myArray[i][j];
      }
    }
  }

void Matrix::toString(){
  for(int i = 0; i < 3; i++){
    for(int j = 0; j < 3; i++){
      cout<<myArray[i][j]<<" ";
    }
    cout<<endl;
  }
}
Matrix Matrix::operator *(const Matrix& sec){
  Matrix result;
  int k = 0;
  for(int i = 0; i < 3; i++){
    for(int l = 0; l < 3 ; l++){
      for(int j = 0; j < 3; j++){
        result.myArray[i][l] += this->myArray[i][j] * sec.myArray[j][i];
      }
    }
  }
  return result;


}
bool Matrix::operator ==(const Matrix& sec){
    int check = 1;
    for(int i = 0; i < 3; i++){
      for(int j = 0; j < 3; j++){
        if(this->myArray[i][j] = sec.myArray[i][j]){
          check = 0;
        }
      }
    }
    if(check == 0){
      return true;
    } else {
      return false;
    }
}

makefile

walk: main.o assignment1.o
        g++ main.o assignment1.o -o walk

main.o: main.cpp
        g++ -c main.cpp

assignment1.o: assignment1.cpp
        g++ -c assignment1.cpp

This is the error is spouts:

g++ main.o assignment1.o -o walk
duplicate symbol 'Matrix::toString()' in:
    main.o
    assignment1.o
duplicate symbol 'Matrix::Matrix()' in:
    main.o
    assignment1.o
duplicate symbol 'Matrix::Matrix()' in:
    main.o
    assignment1.o
duplicate symbol 'Matrix::operator==(Matrix const&)' in:
    main.o
    assignment1.o
duplicate symbol 'Matrix::operator*(Matrix const&)' in:
    main.o
    assignment1.o
ld: 5 duplicate symbols for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [walk] Error 1

I researched and supposedly its because it has already been declared in the .h file but if remove the declaration in the implementation file, it blasts a bunch of errors. So im not sure what is causing the duplicate symbol error

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60

0 Answers0