Header-Handling C++ - not the expected Output! Handling 3 Files ... [SOLVED] | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

Header-Handling C++ - not the expected Output! Handling 3 Files ... [SOLVED]

Hi there! Got 3 files: TestClass.h TestClass.cpp CallingTestClass.cpp TestClass.h: sample Class declared in this Header-file. #ifndef TESTCLASS_H #define TESTCLASS_H #include <string> using namespace std; class TestClass{ public: TestClass(){} string aString; int aInt; void setSum(int a); private: void privateMethod(); int privateSum; }; #endif TestClass.cpp: defining the methods and so on in the sourcefile #include "TestClass.h" #include <iostream> TestClass::TestClass() { aString = "Hi there!"; }; void TestClass::setSum(int a ){ privateSum = a; }; CallingTestClass: creating a obj of the TestClass (Constructor is called and sets the string var to "Hi there!" - then Output this string ... but nothing appears! Seems like the defined constructor of TestClass.cpp isnt called! MS does it the same way, by defining a sample method ... https://docs.microsoft.com/en-us/cpp/cpp/header-files-cpp?view=vs-2019 #include <iostream> #include "TestClass.h" using namespace std; int main(int argc, char* argv[]){ TestClass* objOne = new TestClass(); cout << objOne->aString; return 0; }

16th Aug 2020, 12:21 PM
Oliver
3 Réponses
+ 1
Edited the Tasks.JSON of VSCode to search for multiple *.cpp files in Dir to compile. And after that was doing it the right way, i tried the way u mentioned - via terminal with g++ TestClass.cpp CallingTestClass.cpp -o CallingTestClass Thanks for help! Ps.: to include it like: #include "TestClass.cpp" is probably very uncommon, isnt it?!
17th Aug 2020, 2:07 PM
Oliver
0
Okay - did it as u say! Removed the {} behind the constructor-declaration, that was false, of course. Now - if i want to run the project - it cant compile. Before, it did. When i add: #include"Testclass.cpp" to the CallingTestClass-File - everthing works. But why, isnt that handled the same way by MS, if u check the link? I thought the header implements the declaration, the sourcefile implements the definition and then i include the header-file ... ?!
16th Aug 2020, 3:43 PM
Oliver
0
On Linux using MS Visual Code and GCC (g++).
16th Aug 2020, 3:52 PM
Oliver