+ 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?

28th Dec 2020, 1:54 PM
Issam Seghir
Issam Seghir - avatar
10 Answers
+ 2
Maybe post the codes here?
28th Dec 2020, 2:13 PM
XXX
XXX - avatar
+ 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.
29th Dec 2020, 4:11 PM
XXX
XXX - avatar
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; }
29th Dec 2020, 9:26 AM
Issam Seghir
Issam Seghir - avatar
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() { }
29th Dec 2020, 9:26 AM
Issam Seghir
Issam Seghir - avatar
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
29th Dec 2020, 9:27 AM
Issam Seghir
Issam Seghir - avatar
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.
29th Dec 2020, 9:27 AM
Issam Seghir
Issam Seghir - avatar
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.
29th Dec 2020, 9:28 AM
Issam Seghir
Issam Seghir - avatar
0
Martin Taylor I delete int main And still the same problem
29th Dec 2020, 4:26 PM
Issam Seghir
Issam Seghir - avatar
0
XXX Uh man, thank you very much , u solve my problem
29th Dec 2020, 4:31 PM
Issam Seghir
Issam Seghir - avatar
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.
29th Dec 2020, 6:53 PM
XXX
XXX - avatar