GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Nov-2008, 14:25
nostaque nostaque is offline
New Member
 
Join Date: Oct 2008
Posts: 19
nostaque will become famous soon enough

No type named


Hi all,

I am trying to test every function in the class and stuck with the distance function. I produces an error with no type named. I got no idea what I am doing wrong, appreciate any help form you.

CPP / C++ / C Code:
/*
FILE: Point.h
class provide: Point(an ADT for a point on a two-dimension plane)

CONSTRUCTOR
         Point(double initial_x = 0, double initial_y = 0);
         // Post: the Point has been set to (initial_x, initial_y)


MODIFICATION MEMBER FUNCTION for Point class
         void shift(double x_amount, double y_amount);
         //Post: The Point has been move along x axis by x_amount, y axis by y_amount

         void rotate90();
         //Post: The Point has been rotate 90 degree clockwise around the origin

         int rotate_needed(Point p)
         // Post: return the value of the number of 90 degree clockwise rotations
         //need to move p to upper right quadrant(where x>=0, y>=0)

         void rotation_upper_right(Point &p)
         //Post: The Point p has been rotated 90 degree increment until p has moved
         //to upper right quadrant (where x>=0, y>=0)


CONSTANT MEMBER FUNCTIONS
         doubel get_x() const {return x;}
         //Post: return the x coordinate of the Point

         double get_y() const {return y;}
         //Post: return the y coordinate of the Point


FRIEND FUNCTIONS
         friend istream& Point::operator >>(istream &ins, cost Point &target)
         //Post: the x and y coordinates of target has been read form ins.
         //the return value is the istream ins.


NON-MEMBER FUNCITONS
         double distance(const Point &p1, const Point &p2)
         //Post: return the value of the distance form p1 to p2

         Point mid_point(const Point &p1, const Point &p2)
         //Post: return the value of the Point is halfway between p1 & p2

         bool operator ==(const Point &p1, const Point &p2)
         //Post: return true if p1 and p2 are identical, otherwise, return false

         bool operator !=(const Point &p1, const Point &p2)
         //Post: return true if p1 and p2 are not identical

         Point operator +(const Point &p1, const Point &p2)
         //Post: return the sum of p1 and p2

         ostream& operator <<(ostream &outs, cost Point &source)
         //Post: the x and y coordinate s of source has been written to outs
         //. The return value is the ostream outs.

*/

#ifndef POINT_H
#define POINT_H
#include <iostream.h>
#include <stdlib.h>
class Point
{
   public:
         // CONSTRUCTOR
         Point(double initial_x = 0, double initial_y = 0);

         // MODIFICATION MEMBER FUNCTIONs for Point class
         void shift(double x_amount, double y_amount);
         void rotate90();
         int rotation_needed(Point p);
         void rotation_upper_right(Point &p);

         // CONSTANT MEMBER FUNCTIONS
         double get_x() const {return x;}
         double get_y() const {return y;}

         friend istream& operator >>(istream &ins, Point &target);
         //the istream CAN NOT become a member function, therefore, using keyword FRIEND
         //to access the member variable directly without other mutators.

   private:
         double x;
         double y;
};

//NON-MEMBER FUNCTIONS
double distance(const Point &p1, const Point &p2);
Point mid_point(const Point &p1, const Point &p2);
bool operator ==(const Point &p1, const Point &p2);
bool operator !=(const Point &p1, const Point &p2);
Point operator +(const Point &p1, const Point &p2);
ostream& operator <<(ostream &outs, const Point &source);
#endif // POINT_H
CPP / C++ / C Code:
/*
FILE: Point.cxx
CLASS IMPLIMENTATION: Point
*/
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include "point.h"

Point::Point(double initial_x, double initial_y)
{
   x = initial_x;
   y = initial_y;
}

void Point::shift(double x_amount, double y_amount)
{
   x += x_amount;
   y += y_amount;
}

void Point::rotate90()
{
   double new_x;
   double new_y;

   new_x = y;
   new_y = -x;
   x = new_x;
   y = new_y;
}

int Point::rotation_needed(Point p)
{
   int answer = 0;
   while((p.get_x() < 0) || (p.get_y() < 0))
   {
      p.rotate90();
      answer++;
   }
   return answer;
}

void Point::rotation_upper_right(Point &p)
{
   while((p.get_x() < 0) || (p.get_y() < 0))
   {
      p.rotate90();
   }
}

double distance(const Point &p1,const Point &p2)
{
   double a, b, c_square;
   a = p1.get_x() - p2.get_x();
   b = p1.get_y() - p2.get_y();
   c_square = a*a + b*b;

   return (sqrt(c_square));
}

Point mid_point(const Point& p1, const Point& p2)
{
   double x_midpoint, y_midpoint;

   x_midpoint = (p1.get_x() + p2.get_x()) / 2;
   y_midpoint = (p1.get_y() + p2.get_y()) / 2;

   Point midpoint(x_midpoint, y_midpoint);
   //construct new object midpoint with values of x_midpoint and y_midpoint

   return midpoint;
}

bool operator ==(const Point &p1, const Point &p2)
{
   return ((p1.get_x() == p2.get_x()) && (p1.get_y() == p2.get_y()));
}

bool operator !=(const Point &p1, const Point &p2)
{
   return (!(p1 == p2));
}

Point operator +(const Point &p1, const Point &p2)
{
   double x_sum, y_sum;
   x_sum = p1.get_x() + p2.get_x();
   y_sum = p1.get_y() + p2.get_y();

   Point sum(x_sum, y_sum);
   return sum;
}

ostream& operator <<(ostream &outs, const Point &source)
{
   outs << source.get_x() << " " << source.get_y();
   return outs;
}

istream& operator >>(istream &ins, Point &target)
{
   ins >> target.x >> target.y;
   return ins;
}
CPP / C++ / C Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "point.h"
#include "point.cpp"
using namespace std;

int main()
{
   Point p(1,2);
   Point p1(4,3);
   cout <<"Constructor test: \n";
   cout <<p.get_x() << " " << p.get_y();

   cout << endl;
   cout <<"shift test: \n";
   p.shift(1,2);
   cout <<p.get_x() << " " << p.get_y();

   cout << endl;
   cout <<"rotate90 test: \n";
   p.rotate90();
   cout <<p.get_x() << " " << p.get_y();

   cout << endl;
   cout <<"rotate need test: \n";
   cout <<"number of turn needed: " << p.rotation_needed(p) << endl;
   cout <<"current position: " ;
   cout <<p.get_x() << " " << p.get_y();

   cout << endl;
   cout <<"rotate upper right test: \n";
   p.rotation_upper_right(p);
   cout <<"current position: " <<p.get_x() << " " << p.get_y();


   cout << endl;
   cout <<"distance test: \n";
   cout <<"The distance between 2 points is: " ;
   double d;
   d = distance(p, p1);
   cout << d << endl;


return 0;
}


I am testing on both Dev-c++ and code::block under windows xp sp2, but they produce the same error message.
  #2  
Old 06-Nov-2008, 14:57
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: No type named


Quote:
Originally Posted by nostaque
I am trying to test every function in the class and stuck with the distance function. I produces an error with no type named..

There is a standard C++ library templated function called std::distance. Thus the conflict since you told the compiler to use the std namespace. Here's a reference: cpluspluc.com std::distance

Several possible solutions:

1. Rename your function to something else (maybe pdistance or some such thing).

2. In any place where you actually call your function, instead of

CPP / C++ / C Code:
   d = distance(p, p1);

put something like
CPP / C++ / C Code:
   d = ::distance(p, p1);

There are other ways (such as putting the class in some namespace that you create, then having a using statement that calls it out).

Regards,

Dave
  #3  
Old 06-Nov-2008, 16:46
nostaque nostaque is offline
New Member
 
Join Date: Oct 2008
Posts: 19
nostaque will become famous soon enough

Re: No type named


Quote:
There is a standard C++ library templated function called std::distance. Thus the conflict since you told the compiler to use the std namespace. Here's a reference: cpluspluc.com std::distance

I really don't know distance is a template function. No wonder why it generate the weird error. I know what to do now.

Thanks for your help Dave,
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 20:23
template <class Data> problem, more than one data type qboy C++ Forum 1 13-Oct-2006 17:08
passing locally defined user type to function from function earachefl C++ Forum 1 01-Jul-2006 18:54
[Tutorial] GUI programming with FLTK dsmith FLTK Forum 10 03-Oct-2005 16:41
Error C2143: syntax error : missing ';' before 'type' small_ticket C Programming Language 6 15-May-2004 12:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:46.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.