mrswmmr
mrswmmr

Reputation: 2141

Issue with static functions, g++ says the class hasn't been declared

I am trying to compile this code, and g++ keeps telling me that 'TimeFinder' has not been declared

Header File

#ifndef _TIMEFINDER_H
#define _TIMEFINDER_H
#include <vector>
#include "timefinder.cpp"
using namespace std;
class TimeFinder
{
public:
    static vector<int> time_from_name(string filename);
    static int calc_seconds (vector <int> time);

};
#endif

CPP File

#include "timefinder.h"
using namespace std;
vector<int> TimeFinder::time_from_name(string filename)//Line 14
{
    //Method Body
}

int TimeFinder::calc_seconds (vector <int> time1)//Line 37
{

    //Method Body
}

Why is this going on? I looked at other examples online, and my code seems to match what works for other people...

Edit: The exact error messages are

timefinder.cpp:14: error: ‘TimeFinder’ has not been declared

timefinder.cpp:37: error: ‘TimeFinder’ has not been declared

Edit2: I'm sorry I'm not very good at this yet, but I would like to thank everyone for their suggestions. Hopefully my code quality will begin to improve because of them.

Upvotes: 2

Views: 1858

Answers (2)

David
David

Reputation: 3442

#include "timefinder.cpp"

Header files usually don't include their .cpp implementation file but only whatever other .h file is required to satisfy the definitions required. Usually it's the other way around where .cpp files include the required .h files necessary to fulfill the dependencies.

using namespace std;

This can also cause a lot of havoc and should be avoided in header files. In header files you should always fully qualify your names with their namespace. The reason is if you import this header file into another one that defines the same symbol locally in their namespace like "cout" then both the std::cout and user::cout would clash and cause a compile error or even worse possibly pick the wrong definition and cause a linking error later.

Upvotes: 0

user2100815
user2100815

Reputation:

Do not do this:

#include "timefinder.cpp"

You are pulling your definitions into your header so they appear before their declarations.

There is a lot of other stuff wrong with your code - the use of static members in the first place, passing vectors and strings by value when they should be references, and placing using directives in header files, but removing that #include should fix the immediate problem.

Upvotes: 6

Related Questions