10 Replies - 454 Views - Last Post: 19 October 2008 - 06:41 PM Rate Topic: -----

#1 FrozenSnake  Icon User is offline

  • En man från Sverige!

Reputation: 122
  • View blog
  • Posts: 997
  • Joined: 30-July 08

Constructor problem

Posted 19 October 2008 - 11:43 AM

Straight to the point:

hero.h
class heroClass
{
	public:
		heroClass();
		~heroClass();
		int hp, lvl, exp, gold, magic, weapon, armor, helmet, legs, shield, classID;
		std::string cclass, cname;
				
		void display();
		void charStart();
		void status();
};


hero.cpp
heroClass::heroClass()
{
	this->hp = 10;
	this->lvl = 1; 
	this->exp = 0; 
	this->gold = 0; 
	this->magic = 0; 
	this->weapon = 0; 
	this->armor = 0;
	this->helmet = 0; 
	this->legs = 0;
	this->shield = 0;
	this->classID = 0;	
}


Compiler & Error
Dev-Cpp

multiple definition of `heroClass::heroClass()'
first defined here
ld returned 1 exit status
Makefile.win [Build Error] [advanture.exe] Error 1

Is This A Good Question/Topic? 0
  • +

Replies To: Constructor problem

#2 RattsRule2  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 38
  • Joined: 16-June 08

Re: Constructor problem

Posted 19 October 2008 - 12:09 PM

class heroClass

heroClass::heroClass()

I believe that "class" is a key word therefore you cannot use it in the name of your class. (I am just learning this stuff now.) try

class Hero;

Hero::hero();
Was This Post Helpful? 0
  • +
  • -

#3 FrozenSnake  Icon User is offline

  • En man från Sverige!

Reputation: 122
  • View blog
  • Posts: 997
  • Joined: 30-July 08

Re: Constructor problem

Posted 19 October 2008 - 12:33 PM

View PostRattsRule2, on 19 Oct, 2008 - 12:09 PM, said:

class heroClass

heroClass::heroClass()

I believe that "class" is a key word therefore you cannot use it in the name of your class. (I am just learning this stuff now.) try

class Hero;

Hero::hero();


Nah thats only if you do something like
class class
{

};

or int int; etc :)
int intx = 0; works fine :)

the class works fine it's the constructor that doesnt wanna play with the rest of the code :P
Was This Post Helpful? 0
  • +
  • -

#4 RattsRule2  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 38
  • Joined: 16-June 08

Re: Constructor problem

Posted 19 October 2008 - 01:04 PM

Well, I did say that I am just learning this stuff! I tried.. :blink:
Was This Post Helpful? 0
  • +
  • -

#5 FrozenSnake  Icon User is offline

  • En man från Sverige!

Reputation: 122
  • View blog
  • Posts: 997
  • Joined: 30-July 08

Re: Constructor problem

Posted 19 October 2008 - 01:08 PM

View PostRattsRule2, on 19 Oct, 2008 - 01:04 PM, said:

Well, I did say that I am just learning this stuff! I tried.. :blink:


I didnt mean to be rude :)
Just said that it works as long as you dont use only the word class, int, etc :)
Was This Post Helpful? 0
  • +
  • -

#6 GWatt  Icon User is online

  • member icon

Reputation: 274
  • View blog
  • Posts: 3,072
  • Joined: 01-December 05

Re: Constructor problem

Posted 19 October 2008 - 01:56 PM

Your code compiles fine for me. If you post the entire code for the hero.cpp file we might be able to help you more Sometimes something else in the code causes problems later on.
Was This Post Helpful? 0
  • +
  • -

#7 FrozenSnake  Icon User is offline

  • En man från Sverige!

Reputation: 122
  • View blog
  • Posts: 997
  • Joined: 30-July 08

Re: Constructor problem

Posted 19 October 2008 - 02:24 PM

View PostGWatt, on 19 Oct, 2008 - 01:56 PM, said:

Your code compiles fine for me. If you post the entire code for the hero.cpp file we might be able to help you more Sometimes something else in the code causes problems later on.


It works fine if I do not have the constructor.

Hero.cpp
#include "hero.h"
#include <iostream>
using namespace std;

heroClass::heroClass()
{
	this->hp = 10;
	this->lvl = 1; 
	this->exp = 0; 
	this->gold = 0; 
	this->magic = 0; 
	this->weapon = 0; 
	this->armor = 0;
	this->helmet = 0; 
	this->legs = 0;
	this->shield = 0;
	this->classID = 0;	
}

void heroClass::charStart()
{
	cout << "Enter your name: ";
	cin >> cname;
	
	while(classID < 1)
	{
		cout << "What is your class?\n1. Warrior\n2. Mage\nI am (1-2): ";
		cin >> classID;
		
		if(classID > 2)
			classID = 0;
		if(classID == 1)
			cclass = "Warrior";
		if(classID == 2)
			cclass = "Mage";
	}
	
	cout << "Your name is: " << cname << endl;
	cout << "Your class is: " << cclass << endl;
}

void heroClass::display()
{
	cout << cname;
}

void heroClass::status()
{
	cout << cname << "\nLevel: " << lvl;
}


main.cpp
#include <iostream>
#include "hero.h"

int main()
{
	using namespace std;
	heroClass hero;
	hero.charStart();
	hero.status();
	cin.get();
	cin.get();
}

Was This Post Helpful? 0
  • +
  • -

#8 GWatt  Icon User is online

  • member icon

Reputation: 274
  • View blog
  • Posts: 3,072
  • Joined: 01-December 05

Re: Constructor problem

Posted 19 October 2008 - 02:38 PM

No, that still works for me, except that the compiler expected a definition of ~heroClass. I added one, and that fixed the problem. However, if you accidently leave out the tilde from the destructor the compiler will throw the same error you received. You should check for that.
Was This Post Helpful? 0
  • +
  • -

#9 FrozenSnake  Icon User is offline

  • En man från Sverige!

Reputation: 122
  • View blog
  • Posts: 997
  • Joined: 30-July 08

Re: Constructor problem

Posted 19 October 2008 - 04:01 PM

All it say for me is

multiple definition of `heroClass::heroClass()'
first defined here
ld returned 1 exit status
C:\cpp\advanture\Makefile.win [Build Error] [advanture.exe] Error 1

If i remove the
heroClass::heroClass()
{
	this->hp = 10;
	this->lvl = 1;
	this->exp = 0;
	this->gold = 0;
	this->magic = 0;
	this->weapon = 0;
	this->armor = 0;
	this->helmet = 0;
	this->legs = 0;
	this->shield = 0;
	this->classID = 0;	
}


It compiles and works perfect =/
Was This Post Helpful? 0
  • +
  • -

#10 GWatt  Icon User is online

  • member icon

Reputation: 274
  • View blog
  • Posts: 3,072
  • Joined: 01-December 05

Re: Constructor problem

Posted 19 October 2008 - 05:46 PM

It might be something your IDE is doing. It is also possible that your version of gcc is old.
See if dev-c++ will tell you what version of gcc/mingw you have. The current gcc release version is 4.3.2.

If it's something your compiler is doing, then you have to use the command prompt.
Was This Post Helpful? 0
  • +
  • -

#11 FrozenSnake  Icon User is offline

  • En man från Sverige!

Reputation: 122
  • View blog
  • Posts: 997
  • Joined: 30-July 08

Re: Constructor problem

Posted 19 October 2008 - 06:41 PM

View PostGWatt, on 19 Oct, 2008 - 05:46 PM, said:

It might be something your IDE is doing. It is also possible that your version of gcc is old.
See if dev-c++ will tell you what version of gcc/mingw you have. The current gcc release version is 4.3.2.

If it's something your compiler is doing, then you have to use the command prompt.


I have ver 4.9.9.2 of Dev-Cpp and it said on their homepage that it's with Mingw/GCC 3.4.2
hmm guess I have to figure out how to update the compiler :P

Hmm it works in my C::B, guess I keep using that one :P

This post has been edited by FrozenSnake: 19 October 2008 - 07:10 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1