I'm trying to write the constructor for a class, but it gives me a "no type" compiler error.
I'm aware it's a basic question but the answers i've found for similar questions didn't solve my problem as I don't see where I could've misspelled Scheduler.
///////// Scheduler.cpp file starts here /////////
// all other includes before #include Scheduler.h
#include "Scheduler.h"
int main()
{
graph_t graph = Scheduler::read_graph("filename");
int numberCPUs = 4;
Scheduler s(numberCPUs, graph);
}
namespace Scheduler{
Scheduler(int numberCPUs, graph_t& graph){
for (int i = 0; i<numberCPUs; ++i) {
m_lastTasks.append(-1);
}
}
};
///////// Scheduler.cpp file ends here /////////
///////// Scheduler.h file starts here /////////
#include <vector>
#include <string>
#include <boost/graph/adjacency_list.hpp>
class Scheduler {
// some members defined here
public:
Scheduler(int numCPUs, graph_t& graph);
static graph_t& read_graph(std::string filename);
};
it gives me this error:
Scheduler.cpp:38: error: ISO C++ forbids declaration of ‘Scheduler’ with no type
Scheduler.cpp: In function ‘int Scheduler::Scheduler(int, graph_t&)’:
Scheduler.cpp:41: error: ‘m_lastTasks’ was not declared in this scope
And I don't know why Scheduler is not known... also, why isn't m_numberCPUs known? And why does Scheduler pretend to have "int" as return type?
Oh no... Scheduler is supposed to be the namespace of the class Scheduler... I thought this just saves me writing Scheduler::Scheduler for the definition of each function of the class Scheduler. Removing
namespace Scheduler {
...
};
and adding
Scheduler:: before the definition of every function did the trick. Thanks a lot!
Now I know it was wrong, but could someone tell me why?
Class namespaces can have:
- member functions, which can use the 'this' pointer
- data members, which are associated with an instance
- static members, which are global and disregard any particular instance
- constructors and destructors
- nested classes
Global namespaces can have:
- global functions
- global variables
- static functions/variables, which are per translation-unit
- nested classes and namespaces