Jump to content
  • Advertisement
Sign in to follow this  
samurai_gator

C++ question expected unqualified ID?

This topic is 3272 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


#include <iostream>

class CPoint
{
  public:

   // Constructor to intialize mdX and mdY to zero
   // works with qDefault
   CPoint()
   {
     mdX = 0.0;
     mdY = 0.0;
   }

   // overloading mdX & mdY w/ dX & dY
   // works with
   CPoint( double dX, double dY)
   {
     mdX = dX;
     mdY = dY;

   }

   void print()
   {
     using namespace std;

     cout << "(" << mdX << ", " << mdY << ")" << endl;
   }

   double mdX;
   double mdY;
};

int main()
{
  CPoint qDefault; // creating a qDefault object of type CPoint
  CPoint qCoord;
  qDefault.print();

  CPoint.qCoord( 2.1, -4.8 );
  qCoord.print();
  return 0;
}


I keep getting this error where it says 
 CPoint.qCoord( 2.1, -4.8 );
in the code.

Share this post


Link to post
Share on other sites
Advertisement

CPoint.qCoord( 2.1, -4.8 );

Is illegal.

Are you trying to initialize it ?

If so then this will work :

Cpoint qCoord;

qCoord = CPoint( 2.1 , -4.8 );

Share this post


Link to post
Share on other sites
Quote:
Original post by samurai_gator
*** Source Snippet Removed ***


Try this. Here you use the custom constructor when you initialize your new object of type CPoint with the name qCoord.


#include <iostream>

class CPoint
{
public:

// Constructor to intialize mdX and mdY to zero
// works with qDefault
CPoint::CPoint()
{
mdX = 0.0;
mdY = 0.0;
}

// overloading mdX & mdY w/ dX & dY
// works with
CPoint::CPoint( double dX, double dY)
{
mdX = dX;
mdY = dY;

}

void print()
{
using namespace std;

cout << "(" << mdX << ", " << mdY << ")" << endl;
}

double mdX;
double mdY;
};

int main()
{
CPoint qDefault; // creating a qDefault object of type CPoint
CPoint qCoord( 2.1, -4.8 ); //<---Here is where you call the custom constructor
qDefault.print();

//CPoint.qCoord( 2.1, -4.8 );
qCoord.print();
return 0;
}



Share this post


Link to post
Share on other sites
I wasn't trying to initialize it, even though

qCoord = CPoint( 2.1 , -4.8 );

gets the job done.

The idea is that
qDefault.print();

calls the default constructor and prints out (0,0)

and
qCoord.print();

calls the other constructor and should print out ( 2.1, -4.8 ).

The qCoord constructor should work with the 2nd CPoint constructor ( the one that overloads) and I'm thinking there would be no need to have:

qCoord = CPoint( 2.1 , -4.8 );

because it wasn't necessary in the default constructor.



Share this post


Link to post
Share on other sites
You seem to have some ideas (or at least terminology) about constructors mixed up.

Quote:
Original post by samurai_gator
The idea is that
qDefault.print();

calls the default constructor and prints out (0,0)

That line (qDefault.print();) doesn't call the (default) constructor. CPoint qDefault; is doing that.

Quote:
qCoord.print();

calls the other constructor and should print out ( 2.1, -4.8 ).

Same here - this isn't calling a constructor, instead it's calling the print method on the already-constructed qCoord object.

Quote:
The qCoord constructor...

Not sure if this is just strange wording, but the constructor doesn't belong to the qCoord object instance, but the CPoint class instead. Maybe you could say "the constructor used by qCoord", or perhaps just "default constructor".

Quote:
...should work with the 2nd CPoint constructor

Not sure what you mean by 'work with' - the two constructors can (and are) defined and implemented separately, without depending on each other.

Perhaps you mean something like this? In this case, you might be better served by just having one constructor with default arguments (it is still a default constructor: it can be called with zero arguments), like this:

   // Constructor to intialize mdX and mdY (default: to zero)
CPoint( double dX = 0.0, double dY = 0.0 )
{
mdX = dX;
mdY = dY;
}



Quote:
( the one that overloads)

Your comment for this second constructor, "overloading mdX & mdY w/ dX & dY", is a little confusing: you're not overloading mdX and mdY, but assigning them (to dX and dY). The constructor itself is overloaded, however.

Quote:
and I'm thinking there would be no need to have:

qCoord = CPoint( 2.1 , -4.8 );

because it wasn't necessary in the default constructor.

CPoint a; calls CPoint::CPoint(), the default constructor (can be called with no arguments).

CPoint b( 2.1, -4.8 ); calls CPoint::CPoint(double, double), the constructor that assigns specific values to the point coordinates.

CPoint.qCoord( 2.1, -4.8 ); doesn't make sense :)
The line roughly translates to "call method qCoord with arguments 2.1, -4.8 on the CPoint object instance", which isn't what you're after.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!