14 Replies - 982 Views - Last Post: 02 October 2012 - 12:22 AM Rate Topic: -----

#1 Mgssidley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 01-October 12

"no appropriate default constructor available"

Posted 01 October 2012 - 09:30 PM

New to programming, here's my code for starters:
Salary.h
#include <iostream>
using namespace std;

class Salary
{
public:
	Salary( double, double );
	void setHoursWorked( double );
	double getHoursWorked();
	void setPayRate( double );
	double getPayRate();
	void displayMessage();
	void calcpay();

private:
	double hoursWorked;
	double payRate;
};


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

Salary::Salary( double hours, double rate )
{
	setHoursWorked( hours );
	setPayRate( rate );
}

void Salary::setHoursWorked( double hours )
{
	hoursWorked = hours;
}

double Salary::getHoursWorked()
{
	return hoursWorked;
}

void Salary::setPayRate( double rate )
{
	payRate = rate;
}

double Salary::getPayRate()
{
	return payRate;
}

void Salary::displayMessage()
{
	cout << "Please enter the number of hours worked for the first employee:  " << getPayRate();
}


Source.cpp
#include "Salary.h"

int main()
{
	Salary mySalary;

	mySalary.displayMessage();
}



So there's something wrong with the class I'm trying to create from what I can tell. I get an error saying "error C2512: 'Salary' : no appropriate default constructor available".

Is This A Good Question/Topic? 0
  • +

Replies To: "no appropriate default constructor available"

#2 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: "no appropriate default constructor available"

Posted 01 October 2012 - 10:18 PM

What this means is, in your class, you must have a default constructor, ie, a constructor with no arguments. So, simply add another constructor without any arguments, and you will be good to go.

Also, it is a good idea to include a default constructor whenever you define a constructor with arguments, because some compilers just want a default constructor.
Was This Post Helpful? 0
  • +
  • -

#3 Mgssidley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 01-October 12

Re: "no appropriate default constructor available"

Posted 01 October 2012 - 10:40 PM

View Postaresh, on 01 October 2012 - 10:18 PM, said:

What this means is, in your class, you must have a default constructor, ie, a constructor with no arguments. So, simply add another constructor without any arguments, and you will be good to go.

Also, it is a good idea to include a default constructor whenever you define a constructor with arguments, because some compilers just want a default constructor.


Ok so if I understand correctly, I add another line under my public section? Should it just be for a string or what? I understand what the problem is now, I just don't understand how to fix it.
Was This Post Helpful? 0
  • +
  • -

#4 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: "no appropriate default constructor available"

Posted 01 October 2012 - 11:41 PM

Well, the simplest fix would be to create an empty default constructor. Something like
Salary::Salary(){}


However, if you want, you can also add something in the constructor, but it's not required.

This post has been edited by aresh: 01 October 2012 - 11:42 PM

Was This Post Helpful? 0
  • +
  • -

#5 Mgssidley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 01-October 12

Re: "no appropriate default constructor available"

Posted 01 October 2012 - 11:42 PM

Tell me if I'm wrong, but I tried adding "Salary();" under Public:, above Salary(double);, is that what I was supposed to do? Running it then gives me this error:
error LNK2019: unresolved external symbol "public: __thiscall Salary::Salary(void)" (??0Salary@@QAE@XZ) referenced in function _main
which I can't make any more sense of.
Was This Post Helpful? 0
  • +
  • -

#6 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: "no appropriate default constructor available"

Posted 01 October 2012 - 11:44 PM

Umm... can you please post exactly what you have done?
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3162
  • View blog
  • Posts: 9,547
  • Joined: 05-May 12

Re: "no appropriate default constructor available"

Posted 01 October 2012 - 11:46 PM

He just declared Salary() as a default constructor within the class file header, but didn't define it with the class implementation file. So the linker is looking for the constructor definition at link time.

This post has been edited by Skydiver: 01 October 2012 - 11:47 PM

Was This Post Helpful? 0
  • +
  • -

#8 Mgssidley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 01-October 12

Re: "no appropriate default constructor available"

Posted 01 October 2012 - 11:52 PM

Ok so when I just add "Salary();" as a new line under Public: in my header file, I get the error
error LNK2019: unresolved external symbol "public: __thiscall Salary::Salary(void)" (??0Salary@@QAE@XZ) referenced in function _main


When I put
Salary::Salary(){}
in my "Salary.cpp" file, under "using namespacestd;", I get the same "no appropriate default constructor available" error, as well as
'Salary::Salary(void)' : overloaded member function not found in 'Salary'
and
 'Salary::Salary(void)' : overloaded member function not found in 'Salary'


However, when I add both of those together, it seems to work just fine, the only thing is that my cmd displays "Please enter the number of hours worked for the first employee: -9.2559e+061"

There were two different errors, copied and pasted the same one on accident. But I'm sure that doesn't matter...

Eh not sure how to edit....
Was This Post Helpful? 0
  • +
  • -

#9 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: "no appropriate default constructor available"

Posted 01 October 2012 - 11:53 PM

Well, you must declare the constructor in header, and define it in cpp file. Also, the thing you are noticing might be because you are not initializing everything in your default constructor, so everything has got garbage values.
Was This Post Helpful? 0
  • +
  • -

#10 Mgssidley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 01-October 12

Re: "no appropriate default constructor available"

Posted 01 October 2012 - 11:54 PM

What do I need to do so that it doesn't have any default value?
Was This Post Helpful? 0
  • +
  • -

#11 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: "no appropriate default constructor available"

Posted 01 October 2012 - 11:56 PM

Isn't is obvious? Initialize it!

In your default constructor which you set up right now, initialize everything.
Was This Post Helpful? 0
  • +
  • -

#12 Mgssidley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 01-October 12

Re: "no appropriate default constructor available"

Posted 02 October 2012 - 12:08 AM

Hmmm well the weird value is coming from my displayMessage() function. It's returning "rate" as a double. I meant to use setPayRate(), although it's telling me that "error C2660: 'Salary::setPayRate' : function does not take 0 arguments". Is this because of how I wrote the setPayRate() function?
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3162
  • View blog
  • Posts: 9,547
  • Joined: 05-May 12

Re: "no appropriate default constructor available"

Posted 02 October 2012 - 12:18 AM

Instead of describing what you've done, it'll be easier if you post your code and any error messages you are getting.
Was This Post Helpful? 0
  • +
  • -

#14 Mgssidley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 01-October 12

Re: "no appropriate default constructor available"

Posted 02 October 2012 - 12:22 AM

My bad, here:
#include <iostream>
using namespace std;

class Salary
{
public:
	Salary();
	Salary( double, double );
	void setHoursWorked( double );
	double getHoursWorked();
	void setPayRate( double );
	double getPayRate();
	void displayMessage();
	void calcpay();

private:
	double hoursWorked;
	double payRate;
};
#include <iostream>
#include "Salary.h"
using namespace std;
Salary::Salary()
{
	double x = 0;
}
Salary::Salary( double hours, double rate )
{
	setHoursWorked( hours );
	setPayRate( rate );
}

void Salary::setHoursWorked( double hours )
{
	hoursWorked = hours;
}

double Salary::getHoursWorked()
{
	return hoursWorked;
}

void Salary::setPayRate( double rate )
{
	payRate = rate;
}

double Salary::getPayRate()
{
	return payRate;
}

void Salary::displayMessage()
{
	cout << "Please enter the number of hours worked for the first employee:  " << setPayRate();
}


#include "Salary.h"

int main()
{
	Salary mySalary;

	mySalary.displayMessage();
}


Error: salary.cpp(36): error C2660: 'Salary::setPayRate' : function does not take 0 arguments
Was This Post Helpful? 0
  • +
  • -

#15 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: "no appropriate default constructor available"

Posted 02 October 2012 - 12:22 AM

Well, if you want to use setPayRate(), then first of all, move it outside the cout. Secondly, you must take the rate in a temporary variable, and then pass that as argument to setPayRate.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1