Constructor implementation triggers error: ISO C++ forbids declaration of ‘Scheduler’ with no type

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

///////// 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?

Thanks a lot!
Is Scheduler supposed to be a class or a namespace? It can't be both.
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?

Thanks again
A class namespace is different from a global namespace, do you want me to list examples why?
Yes please, thank you!
Oh, I was half joking, but ok.

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

http://stackoverflow.com/a/3188175/1959975
Topic archived. No new replies allowed.