Hello everyone,
I'm currently in college for Game programming and I'm coming in having known nothing about programming at all. I'm currently learning Object Orientated C++ Programming. I have been at this one problem for two weeks and I'm completely stumped. I currently have three files the main cpp file, the cpp file with all the information, and then the header file. I'm completely stuck on how to properly call the functions into main here are the following codes. This is just the first of many problems I am working on. All help is highly Appreciated as I'm trying to do this as I've gotten little help from my professor. Whom I believe thinks I should figure this out on my own. But with time restraints, it is exceedingly difficult and I fully intend on graduating and getting into the industry. Again thank you ahead of time for any help in helping me fix this and any future questions.

header:

#define Asteroids_h

class Asteroid
{
	private:
	int astSpeed;
	int astSize;
	

	Asteroid () {}
public:

void displayStats(int AsteroidNumber);
void setSize( int astSize);
void setSpeed(int astSpeed);

int setSpeed()
{
	return astSpeed;
}
int setSize()
{
	return astSpeed;
}

};

core file:

#include <iostream>
using namespace std;

class Asteroid
{
	int astSize;
	int astSpeed;
	int AsteroidNumber;
public:
	Asteroid ( int Size, int Speed, int AsteroidNumber )
	{
		setSpeed( Size);
		setSize (Speed);
		
	}
	int getSize()
	{
		return astSize;
	}
	int getSpeed()
	{
		return astSpeed;
	}
	

	void displayStats(int AsteroidNumber)
		
	{
		
		
		cout<< "Asteroids " << endl;
		cout<< " Asteroid number " << AsteroidNumber << endl;
		cout<< " Size " << astSize << endl;
		cout<< " Speed " << astSpeed << endl;
	}
	void setSize(int Size)
	{
		astSize = Size;
	}
	
	void setSpeed(int Speed)
	{
		astSpeed = Speed;
	}
	
};

and the main:

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

int main()

{
	
	Asteroid firstAsteroid();
	Asteroid secondAsteroid();
	
	
	firstAsteroid.setSpeed(20);
	secondAsteroid.setSpeed(3);
	firstAsteroid.displayStats(1);
	secondAsteroid.displayStats(2);

char wait;
cin>> wait;

	
return 0;
}

the current error(s) I have thus far is the following:

asteroidproject\asteroidmain.cpp(9) : error C2664: 'Asteroid::Asteroid(const Asteroid &)' : cannot convert parameter 1 from 'int' to 'const Asteroid &'
1>        Reason: cannot convert from 'int' to 'const Asteroid'
1>        No constructor could take the source type, or constructor overload resolution was ambiguous
1>e:\cit43\asteroidproject\asteroidmain.cpp(10) : error C2664: 'Asteroid::Asteroid(const Asteroid &)' : cannot convert parameter 1 from 'int' to 'const Asteroid &'
1>        Reason: cannot convert from 'int' to 'const Asteroid'
1>        No constructor could take the source type, or constructor overload resolution was ambiguous

Recommended Answers

All 5 Replies

asteroidproject\asteroidmain.cpp(9) : error C2664: 'Asteroid::Asteroid(const Asteroid &)' : cannot convert parameter 1 from 'int' to 'const Asteroid &'
1> Reason: cannot convert from 'int' to 'const Asteroid'
1> No constructor could take the source type, or constructor overload resolution was ambiguous

If you define a parameter as const Asteroid &, you can't pass it an int. Check your parameters.

How would I make it work? Would I have to go back into the class under the public section and make a new variable or function to call in the main file? Sorry if this is a redundant question.

Doesn't relate to error messages, but:

void setSize( int astSize);
void setSpeed(int astSpeed);
int setSpeed()
{	
    return astSpeed;
}
int setSize()

when you declare functions with the same name and different arguments it is called overlaoding the function. This can be a very useful tool. However, in your case I suspect that you don't really want to overload the setx() funtions. I suspect that the setx() functions with return types should really bet getx() functions and you misspelled the function names.

You need to think about what you are trying to accomplish, what variables and/or values you need in the function, and change the parameters accordingly.

FYI, posting on a forum is not the time to turn the brain off and get us to fix it for you. It's your project. We will point you in a direction we think is appropriate but you ultimately need to solve the problem.

This:

Asteroid::Asteroid(const Asteroid &)

looks like it is refering to a copy constructor, which you don't include in your class. (The compiler will supply one for you, which may be okay in simple classes.) Line 9 in main() however is attempting to use a default constructor so I'm not sure why the compiler is refering to a copy constructor. It's probably saying the call is ambiguous because you declared the default copy constructor with private access so trying to access the default constructor using public access won't work so the compiler tried to use the one public constructor it did have, the default copy constructor it wrote, but the arguments don't match, so it flagged an error, though I won't swear to all that.

I would declare the default constructor public access, not private.

In addition, Asteroids.cpp (what you call core file) should look something like this:

#include "Asteroids.h"
using namespace std;

void Asteroids::displayStats(int AsteroidNumber) 	
{  		
    cout<< "Asteroids " << endl;		
    cout<< " Asteroid number " << AsteroidNumber << endl;	
    cout<< " Size " << astSize << endl;		
    cout<< " Speed " << astSpeed << endl;	
}	

void Asteroid::setSize(int Size)	
{		
    astSize = Size;	
} 	

void Asteroid::setSpeed(int Speed)	
{		
     astSpeed = Speed;	
}

Do not redeclare the whole Asteroid class in the cpp file. Do not add new functions to the class in the cpp file that aren't declared in the header file, like you did with these:

Asteroid ( int Size, int Speed, int AsteroidNumber )
int getSize()
int getSpeed()

If you do define a function in the header file, do not define it again in the cpp file.

.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.