compiler ERRor

Why am i getting a error

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1393 Views - Last Post: 07 March 2007 - 08:20 PM Rate Topic: -----

#1 jfkwards  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-March 07

compiler ERRor

Posted 07 March 2007 - 05:58 PM

Why am i getting a ERRor when i run this what does the Error mean?
I think it has do with #3

#include <iostream>
using namespace std;

// Class Definition for the MyInt class
class MyInt
{
	private:
	   int theValue;
	public:
	   MyInt( );
	   MyInt( int );

	   void setValue( int );
	   int getValue ( );
	   float reciprocal( );

	   int bogus;	 // a public data member
};

// The class implementation for the MyInt class
// Constructors
MyInt::MyInt( )
{
	theValue = 0;
	bogus = 5;
}

MyInt::MyInt( int n )
{
	theValue = n;
	bogus = 3;
}

// Getters and setters
void MyInt::setValue( int n )
{
	theValue = n;
}

int MyInt::getValue( )
{
	return theValue;
}

// Calculation functions
float MyInt::reciprocal( )
{
	return 1.0 / theValue;
}

// The Driver
int main ( )
{
	// Test the Constructors
	/* 1 */   MyInt num1;
	/* 2 */   MyInt num2( 2 );

	/* 3 */   cout << "\nThe value of bogus in num1 is " << bogus;

	// Test setter
	/* 4 */   num1.setValue( 5 );
	/* 5 */   cout << "\nnum1's new value = " << num1.getValue( );

	// Test reciprocal
	/* 6 */   cout << "\nnum2's reciprocal = " << num2.reciprocal( );

	cout << "\nTest complete ... hit Enter to exit.";

	system("PAUSE");
				return 0;


Is This A Good Question/Topic? 0
  • +

Replies To: compiler ERRor

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: compiler ERRor

Posted 07 March 2007 - 06:04 PM

Well, you have never specified what the error was. In the future, please post the exact error, as it qill allow our members to more easily debug the problem.

That being said, I can tell you that the following line:
cout << "\nThe value of bogus in num1 is " << bogus;


will generate an error, because you are trying to access a variable that has never been declared or instantiated within the scope that you are trying to use. The variable bogus is a member of the MyInt class, and needs to be referenced as such.
cout << "\nThe value of bogus in num1 is " << num1.bogus;


Also, you may have to include the <cstdlib> header to use the system("pause") call...depends on the compiler.
Was This Post Helpful? 0
  • +
  • -

#3 jfkwards  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-March 07

Re: compiler ERRor

Posted 07 March 2007 - 06:24 PM

wHY AM I GETTING THIS COMIPILER ERROR
.\..\Lab09\integer.cpp:58: error: no match for 'operator<<' in '
std::operator<<(std::basic_ostream<char, _Traits>&, const char*) [with
_Traits = std::char_traits<char>]((&std::cout), "\nThe first object = ") <<
num1'
wHEN I POST THIS CODE
#include <iostream>
using namespace std;

// Class Definition for the MyInt class
class MyInt
{
	private:
	   int theValue;
	public:
	   MyInt( );
	   MyInt( int );

	   void setValue( int );
	   int getValue ( );
	   float reciprocal( );

	   int bogus;	 // a public data member
};

// The class implementation for the MyInt class
// Constructors
MyInt::MyInt( )
{
	theValue = 0;
	bogus = 5;
}

MyInt::MyInt( int n )
{
	theValue = n;
	bogus = 3;
}

// Getters and setters
void MyInt::setValue( int n )
{
	theValue = n;
}

int MyInt::getValue( )
{
	return theValue;
}

// Calculation functions
float MyInt::reciprocal( )
{
	return 1.0 / theValue;
}

// The Driver
int main ( )
{
	// Test the Constructors
	/* 1 */   MyInt num1;
	/* 2 */   MyInt num2( 2 );

	/* 3 */   cout << "\nThe first object = " << num1;

	// Test setter
	/* 4 */   num1.setValue( 5 );
	/* 5 */   cout << "\nnum1's new value = " << num1.getValue( );

	// Test reciprocal
	/* 6 */   cout << "\nnum2's reciprocal = " << num2.reciprocal( );

	cout << "\nTest complete ... hit Enter to exit.";

	system("PAUSE");
				return 0;
}

Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: compiler ERRor

Posted 07 March 2007 - 06:35 PM

Topics merged for continuity...please do not post duplicate topics.

You are getting that error because you are trying to use the << operator to display the value of num1, but num1 is an object made up of several components, not a standard variable type. You must specify which component of num1 you wish to display, like:
cout << "\nThe first object = " << num1.bogus;


Was This Post Helpful? 0
  • +
  • -

#5 jfkwards  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-March 07

Re: compiler ERRor

Posted 07 March 2007 - 06:45 PM

Ok thanks for the help
Was This Post Helpful? 0
  • +
  • -

#6 jfkwards  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-March 07

Re: compiler ERRor

Posted 07 March 2007 - 06:52 PM

Why is bogus visible and theValue is not visible?
What do i need to do to fix this?

#include <iostream>
using namespace std;

// Class Definition for the MyInt class
class MyInt
{
	private:
	   int theValue;
	public:
	   MyInt( );
	   MyInt( int );

	   void setValue( int );
	   int getValue ( );
	   float reciprocal( );

	   int bogus;	 // a public data member
};

// The class implementation for the MyInt class
// Constructors
MyInt::MyInt( )
{
	theValue = 0;
	bogus = 5;
}

MyInt::MyInt( int n )
{
	theValue = n;
	bogus = 3;
}

// Getters and setters
void MyInt::setValue( int n )
{
	theValue = n;
}

int MyInt::getValue( )
{
	return theValue;
}

// Calculation functions
float MyInt::reciprocal( )
{
	return 1.0 / theValue;
}

// The Driver
int main ( )
{
	// Test the Constructors
	/* 1 */   MyInt num1;
	/* 2 */   MyInt num2( 2 );

	/* 3 */   cout << "\nThe first object = " << num1.bogus;
	cout << "\nThe value of theValue in num1 is " << num1.theValue;
	// Test setter
	/* 4 */   num1.setValue( 5 );
	/* 5 */   cout << "\nnum1's new value = " << num1.getValue( );

	// Test reciprocal
	/* 6 */   cout << "\nnum2's reciprocal = " << num2.reciprocal( );

	cout << "\nTest complete ... hit Enter to exit.";

	system("PAUSE");
				return 0;
}

Was This Post Helpful? 0
  • +
  • -

#7 jfkwards  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-March 07

Re: compiler ERRor

Posted 07 March 2007 - 06:56 PM

Why is the variable bogus okay this time, but it wasn't last time? What does the statement num1.bogus mean?
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: compiler ERRor

Posted 07 March 2007 - 07:03 PM

You have two questions:

Quote

Why is bogus visible and theValue is not visible?
What do i need to do to fix this?

One is in the private section, the other in the public. You access the private member, you can either make it public, or more properly create a method within the class to return that value.

Quote

Why is the variable bogus okay this time, but it wasn't last time? What does the statement num1.bogus mean?

In the first version, you were simply trying to access a variable named bogus, which did not exist by itself in the main function. It only exists as a member of the class MyInt.
num1.bogus means that you are accessing the variable named bogus that is a member of the object instance num1.
Was This Post Helpful? 0
  • +
  • -

#9 jfkwards  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-March 07

Re: compiler ERRor

Posted 07 March 2007 - 07:41 PM

how do i create a method within the class to return that value.
Was This Post Helpful? 0
  • +
  • -

#10 jfkwards  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-March 07

Re: compiler ERRor

Posted 07 March 2007 - 07:47 PM

When im compiling the code im getting the error
.\..\..\Lab09\integer.cpp: In function `int main()':
..\..\..\Lab09\integer.cpp:8: error: `int MyInt::theValue' is private
..\..\..\Lab09\integer.cpp:59: error: within this context
What do i need to do to fix my cod so it run
Code:
#include <iostream>
using namespace std;

// Class Definition for the MyInt class
class MyInt
{
	private:
	   int theValue;
	public:
	   MyInt( );
	   MyInt( int );

	   void setValue( int );
	   int getValue ( );
	   float reciprocal( );

	   int bogus;	 // a public data member
};

// The class implementation for the MyInt class
// Constructors
MyInt::MyInt( )
{
	theValue = 0;
	bogus = 5;
}

MyInt::MyInt( int n )
{
	theValue = n;
	bogus = 3;
}

// Getters and setters
void MyInt::setValue( int n )
{
	theValue = n;
}

int MyInt::getValue( )
{
	return theValue;
}

// Calculation functions
float MyInt::reciprocal( )
{
	return 1.0 / theValue;
}

// The Driver
int main ( )
{
	// Test the Constructors
	/* 1 */   MyInt num1;
	/* 2 */   MyInt num2( 2 );

	/* 3 */   cout << "\nThe first object = " << num1.bogus;
	cout << "\nThe value of theValue in num1 is " << num1.theValue;
	// Test setter
	/* 4 */   num1.setValue( 5 );
	/* 5 */   cout << "\nnum1's new value = " << num1.getValue( );

	// Test reciprocal
	/* 6 */   cout << "\nnum2's reciprocal = " << num2.reciprocal( );

	cout << "\nTest complete ... hit Enter to exit.";

	system("PAUSE");
				return 0;
}

Was This Post Helpful? 0
  • +
  • -

#11 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: compiler ERRor

Posted 07 March 2007 - 07:47 PM

You appear to have one...getValue().
Was This Post Helpful? 0
  • +
  • -

#12 jfkwards  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-March 07

Re: compiler ERRor

Posted 07 March 2007 - 07:50 PM

What do you mean?
What would i do to fix it?
Was This Post Helpful? 0
  • +
  • -

#13 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: compiler ERRor

Posted 07 March 2007 - 07:52 PM

Did you write the code for the class included with your posts above?
Was This Post Helpful? 0
  • +
  • -

#14 jfkwards  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-March 07

Re: compiler ERRor

Posted 07 March 2007 - 07:55 PM

yes
Was This Post Helpful? 0
  • +
  • -

#15 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: compiler ERRor

Posted 07 March 2007 - 08:08 PM

Well, then perhaps you may wish to look at the function getValue that you have written. getValue is a method that accesses and returns the value of the private data member theValue. You use it a couple of lines below where the error occurs. Your error occurs here:
cout << "\nThe value of theValue in num1 is " << num1.theValue;


because you are trying to directly access a private data member. Instead, you need to have a method to access it. You asked how to create a method to return that value, but you have already written it - it is getValue(). You use it successfully here:
cout << "\nnum1's new value = " << num1.getValue( );


See?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2