+ 1
Header file problem
I have a problem in separating class file in vs code The header file don't work when I compile .. the class object of header file dont work and compiler show me error any idea can help me to fix this problem?
22 Answers
+ 2
Maybe post the codes here?
+ 1
Martin Taylor I don't understand. The asker has only declared `int main()` in Class.cpp
Issam Seghir
I think the problem is that in `console.cpp`, you have only included "Class.h", where the Car2 class is declared , but you have not included "Class.cpp" where you have defined the methods of the Car2 class.
0
My main file : (consol.cpp)
đđ
// Consol.cpp : This file contains the 'main' function. Program execution begins and ends there.
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
#include <iostream>
#include <string>
#include "Class.h"
using namespace std;
int main()
{
Car2 car2;
cout << ".............................................................................." << endl
<< "This is Car 2 in Header file with initial value to Constructor Car2();" << endl
<< " name of car 2 : "
<< car2.printname2() << endl
<< " model of car 2 : " << car2.printmod2() << endl
<< ".............................................................................."
<< endl; return 0;
}
0
My separated class file :
Class.cpp
đđ
#include <iostream>
#include <string>
#include "Class.h"
int main();
void Car2::scanname2(string name2)
{
name2 = name;
}
string Car2::printname2() {
return name;
}
void Car2:: scanmod2(int model2) {
model2 = model;
}
int Car2:: printmod2() {
return model;
}
Car2::Car2() : model(1598), name ("Volkswagen")
{
} Car2::~Car2()
{
}
0
Header file ( Class.h) :
đđ
// #pragma once
#include <string>
using namespace std ;
#ifndef CLASS_H
#define CLASS_H
class Car2
{
private:
int model;
string name;
public:
void scanname2(string name2);
string printname2();
void scanmod2(int model2);
int printmod2();
Car2(); // constructor it's special method ( no return , same name of class)
~Car2();
};
#endif
0
Error message in console.cpp :
â ď¸â ď¸
> Executing task: C/C++: g++.exe build active file <
Starting build...
Build finished with errors(s):
C:\Users\S8A8A~1.ISS\AppData\Local\Temp\ccqYXWsM.o: In function `main':
C:/Users/S.issam/Desktop/C++/consolaplication2/consol.cpp:141: undefined reference to `Car2::Car2()'
C:/Users/S.issam/Desktop/C++/consolaplication2/consol.cpp:145: undefined reference to `Car2::printname2[abi:cxx11]()'
C:/Users/S.issam/Desktop/C++/consolaplication2/consol.cpp:146: undefined reference to `Car2::printmod2()'
C:/Users/S.issam/Desktop/C++/consolaplication2/consol.cpp:141: undefined reference to `Car2::~Car2()'
C:/Users/S.issam/Desktop/C++/consolaplication2/consol.cpp:141: undefined reference to `Car2::~Car2()'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: -1.
0
Error message in class.cpp :
â ď¸â ď¸
> Executing task: C/C++: g++.exe build active file <
Starting build...
Build finished with errors(s):
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: -1.
0
Martin Taylor I delete int main
And still the same problem
0
XXX Uh man, thank you very much , u solve my problem
0
Martin Taylor sorry if I'm wrong, but in Consol.cpp, the OP has only declared the main function, not defined it right?(sorry also if I've got the terms wrong. I don't know what a function declaration without a definition is called)
`int main();`
About the second error, yeah I forgot to mention that and also, I didn't know what that meant until you told in your answer.
Issam Seghir you should see Martin Taylor's answer and the code given by him.
(Also, a question for Martin Taylor, don't mentions work on the web version of SL or is it a bug that in your answers I always see "@XXX" instead of a mention)
EDIT:
just read your latest answer and sorry, I thought that was the problem because in the error message, only the methods that were called in the main function were shown. So I just thought that it is because their definition is not complete.