0

Files not linking in C++

So I'm taking the C++ Intermediate Course. I use Android mobile. I was taking the "Seperate Files for Classes" lesson in the course where they said about creating seperate file for the prototypes (.h) and creating a source file that uses that file(.cpp). I did it in QuickEdit and tried some other apps, but all of them were returning error that they could not find the file. Please note before replying: • My files were in the same folder together, no different folders or subfolders. • I'm using Android Phone • The file name for linking and the file's name are exactly the same, with matching case When I run the code, I get this error: main.cpp:2:10: fatal error: 'MyClass.h' file not found #include "MyClass.h" ^~~~~~~~~~~ 1 error generated.

25th Nov 2024, 3:03 AM
Afnan Irtesum Chowdhury
Afnan Irtesum Chowdhury - avatar
8 Answers
+ 2
I tried to simulate your situation, and it's true QuickEdit didn't get the thing done, not sure why though. But good news is - it does work in Cxxdroid app. Just a class declaration in header (with only 1 public method for testing), definition of the public method in source, object instantiation and the public method call in source (main function), that's it :)
25th Nov 2024, 9:46 AM
Ipang
+ 3
Hi Afnan, Yes, to my learner level of understanding, that's an excellent example, and it's good to hear it works on your end. Nice code formatting and bit of code comments as reminder, for latter reads... Good job! keep it up!
25th Nov 2024, 3:26 PM
Ipang
+ 2
Since it is a "file not found" error, then the problem has to be with finding the file. If your main.cpp is in a subfolder, maybe add that to the include path and see if that helps. If you are using Linux, make sure your filename is MyClass.h and not myclass.h. Make sure the filename matches and matches case.
25th Nov 2024, 4:02 AM
Jerry Hobby
Jerry Hobby - avatar
+ 2
If you are on Linux, you should probably add it this way..... #include "./MyClass.h"
25th Nov 2024, 6:59 AM
Jan
Jan - avatar
+ 2
Ipang Tysm, I followed your method and it worked. But just for clarification still, this is how I do it right?: MyClass.h: #ifndef MYCLASS_H #define MYCLASS_H class MyClass { public: MyClass(); // Constructor declaration ~MyClass(); // Destructor declaration void sayHello(); // Test method to call in main }; #endif // MYCLASS_H MyClass.cpp: #include <iostream> #include "MyClass.h" using namespace std; // Constructor definition MyClass::MyClass() { cout << "MyClass created!" << endl; } // Destructor definition MyClass::~MyClass() { cout << "MyClass destroyed!" << endl; } // sayHello method definition void MyClass::sayHello() { cout << "Hello from MyClass!" << endl; } int main() { MyClass obj; // Create MyClass object obj.sayHello(); // Call the sayHello method return 0; }
25th Nov 2024, 11:57 AM
Afnan Irtesum Chowdhury
Afnan Irtesum Chowdhury - avatar
+ 1
Afnan Irtesum Chowdhury what app are you using? Folder access is very restricted on Android phones. It might be a permissions issue. If you read the lessons carefully, Sololearn assumes you are using CodeBlocks on a pc.There is even an intruction on how to set it up. That part was not meant to be run on Sololearn. You can learn the basics in your phone, but to really complie and run programs, you will need a pc or a laptop.
25th Nov 2024, 8:57 AM
Bob_Li
Bob_Li - avatar
0
Jan Jerry Hobby I'm on Android, Phone. And the files are in the same folder together as I already said
25th Nov 2024, 7:15 AM
Afnan Irtesum Chowdhury
Afnan Irtesum Chowdhury - avatar
0
Afnan Irtesum Chowdhury Android is based on Linux, even though it's written in Java, so you should probably still use the method I mentioned.
25th Nov 2024, 8:16 AM
Jan
Jan - avatar