What's new

Someone help!

Eagle

aka Alshain
Moderator
This is my C++ homework and its driving me fricken crazy.

Code:
class Counter
{
   public:

      Counter( );           // default constructor - initializes count to 0
      Counter ( int v);    //  constructor - initializes count to v
      void increment( int v = 1);     // increments count by argument value
      void decrement (int v = 1);     // decrements count by argument value
      void reset ( );                 // resets count to 0
      int get_count ( ) const;        // returns count

   private:
      int count;
};

Code:
int Counter::get_count()
{
	return count;
}


Error:
C:\Documents and Settings\Desktop\New Folder\Class\class.cpp(31) : error C2511: 'get_count' : overloaded member function 'const int (void)' not found in 'Counter'
c:\documents and settings\desktop\new folder\class\counter.h(6) : see declaration of 'Counter'


He gave us the header file, I can only change the implementation. What am I doing wrong?
 
Last edited:

Cyberman

Moderator
Moderator
Eagle said:
This is my C++ homework and its driving me fricken crazy.

Code:
class Counter
{
   public:

      Counter( );           // default constructor - initializes count to 0
      Counter ( int v);    //  constructor - initializes count to v
      void increment( int v = 1);     // increments count by argument value
      void decrement (int v = 1);     // decrements count by argument value
      void reset ( );                 // resets count to 0
      int get_count ( ) const;        // returns count

   private:
      int count;
};

Code:
int Counter::get_count()
{
	return count;
}


Error:
C:\Documents and Settings\Kevin Seiden\Desktop\New Folder\Class\class.cpp(31) : error C2511: 'get_count' : overloaded member function 'const int (void)' not found in 'Counter'
c:\documents and settings\kevin seiden\desktop\new folder\class\counter.h(6) : see declaration of 'Counter'


He gave us the header file, I can only change the implementation. What am I doing wrong?

Hmmm well for kicks I would comment out the code you have in your module and stuff into the header file a slightly different definition like.

Code:
  int Getcount() { return count; }
and see what errors you get.. that should work about the same and it is an inline function that does not expose count. Not sure what the heck you are defining it as const that somehow looks wrong too me :) (head is tired from RegisterFileFormat CRAP).

Cyb
 
OP
Eagle

Eagle

aka Alshain
Moderator
Re: Re: Someone help!

Cyberman said:
Hmmm well for kicks I would comment out the code you have in your module and stuff into the header file a slightly different definition like.

Code:
  int Getcount() { return count; }
and see what errors you get.. that should work about the same and it is an inline function that does not expose count. Not sure what the heck you are defining it as const that somehow looks wrong too me :) (head is tired from RegisterFileFormat CRAP).

Cyb

I cant do that. He gave us the Class definition and we have to implement it. Thats the assignment. We cant change the class definition.

I dont quite understand what your saying when you say "expose count"
 
OP
Eagle

Eagle

aka Alshain
Moderator
I figured it out. I was missing a const

Code:
int Counter::get_count() const
{
	return count;
}
 

Cyberman

Moderator
Moderator
Well that was the other alternative (LOL) glad you got it fixed. It really wasn't that bad then :)

Too bad I couldn't see that :(
 

Doomulation

?????????????????????????
Eagle said:
I figured it out. I was missing a const

Code:
int Counter::get_count() const
{
	return count;
}
I was just about to say that ^_^
If c++ expects nothing to be changed within a call, it looks for a const function. Gee, this is what makes c++ hard ;)
 

Cyberman

Moderator
Moderator
Doomulation said:
I was just about to say that ^_^
If c++ expects nothing to be changed within a call, it looks for a const function. Gee, this is what makes c++ hard ;)
Actually that is what makes C++ so nice, you have good solid type casting so you don't get weird errors. OOP is an extension of Structured programing. However if you aren't very structured C++ won't like you. thus although you can be a poor C++ code writter you still are forced to do things at least somewhat correctly ;)

Cyb
 

Doomulation

?????????????????????????
Yes, that is one of the powers on c++.
It's good, and i don't complain. But it can be pain in the ass, if you don't make it right :p
 

Cyberman

Moderator
Moderator
Doomulation said:
Yes, that is one of the powers on c++.
It's good, and i don't complain. But it can be pain in the ass, if you don't make it right :p

Things that make be go nuts actually is overloading the '='/ assignment operator (ugh) which for example might actually be over loaded to see if *this is null or not (boggle) and if it's not makes a copy of the object instead of assigning the pointer..

Behavior in C++ can be .. interesting to say the least :)


Cyb
 

Top