0
I created a class called Board, make the prototypes in the file Tray. h and keep the code on Board.cpp, during the study I saw that just adding the Tray. h at the index.cpp would already be possible to instantiate the Class board (new Board(10,20,1)). However in my code it accuses error, and only works if I include directly the file Board.cpp.
Error:
[Running] cd "c:\xampp\htdocs\grimoriec++\" && g++ index.cpp -o index && "c:\xampp\htdocs\grimoriec++\"index
C:\Users\kauen\AppData\Local\Temp\cchnmOmm.o:index.cpp:(.text+0x40): undefined reference to `Tabuleiro::Tabuleiro(int, int, int)'
C:\Users\kauen\AppData\Local\Temp\cchnmOmm.o:index.cpp:(.text+0x64): undefined reference to `Tabuleiro::to_string()'
collect2.exe: error: ld returned 1 exit status
I want to know if the correct way to add a class from another file is by cpp file. or by file. h, and if it is the second option what I have to change in my code for include to work ?
index.cpp
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <iostream>
#include <memory>
#include "Tabuleiro.h"
using namespace std;
Tabuleiro::Tabuleiro(int x,int y,int z){
this->tam_x = x;
this->tam_y = y;
this->tam_z = z;
};
void Tabuleiro::to_string(){
cout << this->tam_x << endl;
}
Board.cpp
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "Tabuleiro.h"
using namespace std;
Tabuleiro::Tabuleiro(int x,int y,int z){
this->tam_x = x;
this->tam_y = y;
this->tam_z = z;
};
void Tabuleiro::to_string(){
cout << this->tam_x << endl;
}
Tray. h
#ifndef TABULEIRO_H
#define TABULEIRO_H
class Tabuleiro{
public:
int tam_x;
int tam_y;
int tam_z;
Tabuleiro(int x,int y,int z);
void to_string();
};
#endif // TABULEIRO_H
In the build you must include all . cpp: g++ index.cpp Board.cpp -o index
– zentrunix
Thank you very much. It now worked I am searching the commands to compile all the Cpps of a folder
– Kaue Alves