# C++ Copy Constructor Help



## dcf-joe (Feb 11, 2010)

Our instructor flew over this topic earlier today and now I have an assignment over the stuff. I believe it has something to do with copy constructing.

Assignment:


> An int data field named value that stores the int value represented by this object
> A constructor that creates a MyInteger object for the specified int value
> A get function that returns the int value
> Functions isEven(), isOdd(), isPrime() that return true if the value is even, odd, or prime, respectively
> ...



Debug information:


> 1>------ Build started: Project: MyInteger, Configuration: Debug Win32 ------
> 1>Compiling...
> 1>MyInteger.cpp
> 1>c:\program files\rocketdock\software\visual c++ projects\myinteger\myinteger\myinteger.cpp(53) : error C2511: 'bool MyInteger::isEven(MyInteger &)' : overloaded member function not found in 'MyInteger'
> ...



Class Header:

```
#ifndef MYINTEGER_H
#define MYINTEGER_H

#include <iostream>
#include <string>
using namespace std;

class MyInteger
{
// Set value to private
private:
	int value;

public:
	// MyInteger constructors
	MyInteger();
	MyInteger(int);
	
	// Return the private value
	int getValue();
	
	// Test if value is even, odd, or prime
	bool isEven();
	bool isOdd();
	bool isPrime();
	
	// Test if a specified value is even, odd, or prime
	static bool isEven(int);
	static bool isOdd(int);
	static bool isPrime(int);
	
	// Test if a specified value is even, odd, or prime
	static bool isEven(MyInteger);
	static bool isOdd(MyInteger);
	static bool isPrime(MyInteger);
	
	// Test if value in object is equal to a specified value
	bool equals(int);
	bool equals(MyInteger);

	// Convert string to int value
	static string parseInt(string);
};

#endif
```

Class Declaration:

```
#include <iostream>
#include <stdlib.h>
#include <string>
#include "MyInteger.h"
using namespace std;

MyInteger::MyInteger()
{
	value = 6;
}

MyInteger::MyInteger(int newValue)
{
	value = newValue;
}

int MyInteger::getValue()
{
	return value;
}

bool MyInteger::isEven()
{
	return (value % 2 == 0);
}

bool MyInteger::isOdd()
{
	return (value % 2 != 0);
}

bool MyInteger::isPrime()
{
	return (value % 2 == 0 || value % 3 == 0 || value % 5 == 0);
}

bool MyInteger::isEven(int newValue)
{
	return (newValue % 2 == 0);
}

bool MyInteger::isOdd(int newValue)
{
	return (newValue % 2 != 0);
}

bool MyInteger::isPrime(int newValue)
{
	return (newValue % 2 == 0 || newValue % 3 == 0 || newValue % 5 == 0);
}

bool MyInteger::isEven(MyInteger &MyInteger)
{
	return (number1 % 2 == 0);
}

bool MyInteger::isOdd(MyInteger &MyInteger)
{
	return (number1 % 2 != 0);
}

bool MyInteger::isPrime(MyInteger &MyInteger)
{
	return (number1 % 2 == 0 || 
		    number1 % 3 == 0 || 
			number1 % 5 == 0);
}

bool MyInteger::equals(int newValue)
{
	return (value == newValue);
}

bool MyInteger::equals(MyInteger number2(int newValue))
{
	return (number1.getValue() == number2.getValue());
}

int MyInteger::parseInt(string newString)
{
	int stringNumber = atoi(newString.c_str());
	return (stringNumber);
}
```

Int Main():

```
#include <iostream>
#include "MyInteger.h"
using namespace std;

int main()
{
	MyInteger number1(10);
	cout << "The Number Stored In number1 Is:  " 
		 << number1.getValue();
	
	cout << "Is " << number1.getValue() << " Even?  " 
		 << number1.isEven();
	
	cout << "Is " << number1.getValue() << " Odd?  " 
		 << number1.isOdd();
	
	cout << "Is " << number1.getValue() << " Prime?  " 
		 << number1.isPrime();

	MyInteger::isEven(25);
	MyInteger::isOdd(25);
	MyInteger::isPrime(25);

	MyInteger::isEven(32);
	MyInteger::isOdd(32);
	MyInteger::isPrime(32);

	cout << "Does Value In Object Equal A New Value?  "
		 << number1.equals(121);

	cout << "Does number2 Equal number1?  "
		 << number1.equals(279);

	MyInteger::parseInt("121");

	return 0;
}
```

My instructor requires that all classes be created with at least three files, one header and two .cpp's. I do not understand at all what these errors messages mean.


----------



## Clement (Feb 12, 2010)

You are passing too few, too many, or incorrectly declared data to these functions. Overloaded usually refers to too many arguments that the function is not declared to handle, but it is also a generic return code that can also refer to incorrectly defined variables (with many older compilers).

Hints: Your functions are lacking access to some variables and/or you are passing incorrect values to your functions.

Overloaded member functions are exactly that. Sorry, but it doesn't get any simpler than that. Maybe you should study the terminology a tad more, I think it will help you.

EDIT: Btw your debug report tells you to "see declaration of". You should listen to it  Thats where the majority of your problems lay, there are a few elsewhere also.

The way you are passing arguments around is *very confusing*, you should follow the path yourself and make it more simplified.

Efficiency is akin to simplicity. IMHO you should aim for that.

Have you been taught how to use pointers yet? They would be very useful in your situation.

I can do no more but to tell you what to do, but you would miss the point and have much more trouble later, these are basic ideals you need to understand as a Software engineer. Even if you aren't becoming one, the concepts are important.

Good luck!


----------



## dcf-joe (Feb 13, 2010)

Thank You Clement. I used your hint, and I now have four out of six errors fixed! I am currently working on the other two.

I totally agree with you, I have to learn the language for myself, or I will be no good as a programmer or software engineer.


----------



## Clement (Feb 13, 2010)

dcf-joe said:


> Thank You Clement. I used your hint, and I now have four out of six errors fixed! I am currently working on the other two.
> 
> I totally agree with you, I have to learn the language for myself, or I will be no good as a programmer or software engineer.



Glad I could help. 

Want another tip?

Try being unique with your code! Think for yourself!

That is, if you want to stand out in the crowd.

The language may seem constricting at first, but in due time, you will learn that they really aren't! The men whom created this language over the years from B, are nothing short of genius!

I am a big fan of using assembly


----------

