need help about to scream lol

idk why this wont work. it keeps telling me."[Error] invalid use of 'Rat::Rat'"

main file
1
2
3
4
5
6
7
8
9
10
11
12
  
#include <iostream>
#include "Rat.h"

using namespace std;

int main (){
	
Rat bob;
bob.Rat(6)
cout << bob.namerat() << endl;
};

my header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef RAT_H
#define RAT_H
#include <iostream>

using namespace std;
class Rat{

private:
int age;

public:
Rat();
Rat( int Newage ) {age= Newage;};

int namerat();
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

And my cpp...#include "Rat.h"
#include <iostream>

using namespace std;

Rat::Rat(int Newage){
	age = Newage;
}

Rat::namerat(){
	return age;
}


THx guys



Last edited on
Rat::namerat(){
Where is the return value.
?
Look at your function prototype:
int namerat();

Notice the return type, an int. Where have you defined this return type in your function implementation?

now it is saying "[Error] redefinition of 'Rat::Rat(int)'"

gg :(
You really need to review your textbook and your course notes on how to declare and implement class member functions. This link may also help: http://www.cplusplus.com/doc/tutorial/classes/

It's okay to scream. (I recommend you go do it and come back.)

The way things are written is confusing because a constructor is a funny kind of function that does not have a return type. A destructor doesn't have one either.

All other functions must always have their return type listed.

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
#include <iostream>
using namespace std;


class Greeting
{
  string name;

  // I'm a constructor, so I have no return type. (I return a Greeting.)
  Greeting( string a_name );
  
  // I'm a normal function (well, a method), so I must have a return type.
  void greet();
};


// Now the constructor's definition. Again, I have no return type.
Greeting::Greeting( string a_name )
{
  name = a_name;
  // (because I automatically return a Greeting)
}


// And again, I'm a normal method, so I must have a return type.
void greet()
{
  cout << "Hello " << name << "!\n";
  // (void functions don't need a "return" statement)
}


int main()  // (see my return type? it's an int)
{
  Greeting a( "Niles" );
  a.greet();
  return 0;
}

Hope this helps.

lots of edits to make the code something you can actually run
Last edited on
Topic archived. No new replies allowed.