Thread: What is wrong

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    8

    What is wrong

    what is wrong in this code? when i compile i get the following error:
    Error 'newcoordinate':undeclared identifier
    Code:
    #include "stdafx.h"
    #include <iostream>
    using std::cout;
    using std::cin;
    //using std::endl;
    
    class Test
    {
    private:
    	double coordinate;
    public:
    	Test();
    	~Test();
    	double getcoordinate();
    	void setcoordinate(double newcoordinate);
    };
    
    Test::Test()
    {
    }
    Test::~Test()
    {
    }
    
    double Test::getcoordinate()
    {
    	return coordinate;
    }
    
    void Test::setcoordinate(double newcoordinate)
    {
    	
    	cout<<"please give coordinate"<<'\n';
    	cin>>newcoordinate;
    	coordinate = newcoordinate;
    	cout<<"The number you gave is"<<coordinate;
    
    	
    }
    
    int main()
    {
    	Test number;
    	number.setcoordinate(newcoordinate);
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The usage of newcordinate in Test::setcoordinate() is local to that function. To use a variable with that name in main(), you need to change main() to read something like;
    Code:
    int main()
    {
    	Test number;
            double newcoordinate;
    	number.setcoordinate(newcoordinate);
    	return 0;
    }
    That will shut up the compiler, at least.

    You're mixing up a couple of practical things in your code though; I suggest removing responsibility from your Test class to read value of a coordinate. That means, firstly, changing Test::setcoordinate() to something like;
    Code:
    void Test::setcoordinate(double newcoordinate)
    {
    	coordinate = newcoordinate;
    }
    and changing main() to;
    Code:
    int main()
    {
    	Test number;
            double newcoordinate;
    
    	
    	cout<<"please give coordinate"<<'\n';
    	cin>>newcoordinate;
    
    	number.setcoordinate(newcoordinate);
    
            cout<<"The number you gave is"<< number.getcoordinate() 
                  << " which should be the same as " << newcoordinate << '\n';
    
    	return 0;
    }

  3. #3
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    > number.setcoordinate(newcoordinate);

    Where is the value of newcoordinate?

    Actually the member function setcoordinate is a bir strange! You call it by a value (which is then assigned to the private varialbe coordinate), but in the function, you prompt the user to enter another value for this purpose!?

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    8
    Thnk you all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM