My project is seperated into two files(a head file and a implementation file). But when i run the codes it comes the errors as follows:

my codes are as follows:
//first file (first_homework.cpp)

//second file(first_homgwork.h.cpp)
I will appreciate it if anyone can be capable to give me a hand.
Thanks for your help!

Attachments errors.png 28.79 KB first_file.png 10.36 KB
1 Year
Discussion Span
Last Post by New Jack

The error is a very simple one: you misspelled the header name in your program source file. Just change the 'g' to an 'e' and it should go through correctly.

BTW, I would do four other things. First, I would remove the using namespace std; from the header file, and explicitly scope the standard library functions and objects using std::, like so:

std::cout << "The area of the Circle is " << S << std::endl;

Otherwise, the using directive carries over to every file that includes that header, which causes an effect called 'namespace pollution', where you start to get conflicts between the names of functions and classes. The whole purpose of namespaces is to help avoid naming collisions, and unrestricted use of the using directive undermines that. It is OK to use using namespace std; in the main program itself, as it won't leak out to any other parts of the program, but having it in a header or a class implementation file is definitely a Bad Thing.

Second, I would add #include guards to your header file, to protect it from being included more than once.


#include <iostream>

class Circle 
// the rest of your header goes here
// ...

};  // end of class Rectangle 


It isn't really that important for this instance, but it is a good habit to get into.

Third, I would move the actual methods of the three classes to a separate implementation file, first_homework.cpp, and have only the declarations of the member variables and member functions in the class declaration.



class Circle 
    void Get();
    void Calculate();
    void Print();
    double R, S;

// and so forth...


#include <iostream>
#include "first_homework.h"

void Circle::Get()
    std::cin >> R;

//  etc...

The :: is called the scoping operator, and it tells the compiler which class (or namespace) the member function belongs to. You need to ensure that the new implementation file is part of your command invocation and/or Makefile/Project file, to be sure that it gets linked into the rest of the program.

Finally, I would explicitly add #include <iostream> to the main program file; it is a poor practice to assume that the header will have any other headers you might need, even if you know they are there.

Edited by Schol-R-LEA


Thanks for your guidance!
firsthomework.cpp.pngfirst_homework.h.cpp.png I have changed the 'g' to 'e',however I have no idea why it comes like that"[Error] C:\Users\Run!Run!Run~~\Documents\C-Free\Temp\first_homework.cpp:1:28: FIRSTHOMEWORK_H : No such file or directory"
I think there must be something wrong with my ProjectGroup shown in the picture....

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.