I seem to be having an issue with using multiple class constructors? I did it for one other program and didn't have an issue. I can't seem to find the bug.

Since it is complaining about multiple constructors it won't let me make Plorg1 in the 3rd set of code. Complaining that it doesn't know which constructor to use. Any help is appreciated. Ty

// plorg.h - class declaration for plorg class

#include <cstring>
#include <string>

namespace PLORG
{
	class Plorg
	{
		private:
			std::string name;
			int index;
		public:
			Plorg() { name = "Plorga" ; index = 50; }
			Plorg(const char * str = "50", const int n = 50);			
			~Plorg();
			void changePlorgCI(const int n);
			void showPlorg() const;
	};
} // end of namespace
// plorg.cpp - function definitions of plorg class

#include "stdafx.h"
#include "plorg.h"
#include <iostream>
#include <cstring>

namespace PLORG
{

	Plorg::Plorg(const char * str, const int n)
	{
		char temp[19];
		strncpy_s(temp, str, 19);
		temp[19] = '\0';
		name = temp;
		index = n;
	}

	Plorg::~Plorg()
	{
		std::cout << "Deconstructor Called!" << std::endl;
	}

	void Plorg::changePlorgCI(const int n)
	{
		index = n;
	}

	void Plorg::showPlorg() const
	{
		std::cout << "Name is " << name << std::endl;
		std::cout << "Index is " << index << std::endl;
	}
} // end of PLORG namespace
// Exc_7.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "plorg.h"
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
	PLORG::Plorg Plorg1;
	PLORG::Plorg Plorg2("Testing Plorg 2 cut off", 30);
	PLORG::Plorg Plorg3("Testing 3");
	Plorg2.showPlorg();
	Plorg3.showPlorg();
	Plorg3.changePlorgCI(20);
	Plorg1.changePlorgCI(30);
	Plorg1.showPlorg();
	Plorg2.showPlorg();
	Plorg3.showPlorg();

	system("PAUSE");
	return 0;
}

The problem is that you have default values for both parameters of your parametrized constructor. This means that this parametrized constructor can be called without any parameters, just like the default constructor. This is where the ambiguity comes from, if you create an object without parameters to the constructor, should the default constructor be called or should the parametrized constructor be called with default parameters. To fix it, you can either make at least one parameter of the parametrized constructor without a default value, or you can take out the default constructor (assuming that the default parameter values of your other constructor correspond to what you want to happen when an object is created without parameters).

Ah ok, got it to work, can't believe that was messing me up, but the logic of it makes sense.

Tyvm

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.