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)
second_file.png
I will appreciate it if anyone can be capable to give me a hand.
Thanks for your help!

Recommended Answers

All 4 Replies

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.

#ifndef FIRSTHOMEWORK_H
#define FIRSTHOMEWORK_H 1

#include <iostream>

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

};  // end of class Rectangle 

#endif

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.

first_homework.h

#ifndef FIRSTHOMEWORK_H
#define FIRSTHOMEWORK_H 1

class Circle 
{
public:
    void Get();
    void Calculate();
    void Print();
private:
    double R, S;
};

// and so forth...
#endif    

first_homework.cpp

#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.

Thanks for your guidance!
error2.pngproject.png
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....

There should not be a '.cpp' extension on the header file, just '.h'.

Thanks very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.