Jump to content

Default Constructors and Initialization Lists

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
LukeyJ

LukeyJ

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
Hi, I'm trying to get a default constructor working so it can work with 0 or 4 values, so I used a constructor with 4 values that could be used or default values if there was none.

#include <iostream>

#include <fstream>

using namespace std;


class tester

{

public:

	int field1;

	int field2;

	int field3;

	int field4;


	tester (int var1, int var2, int var3, int var4);

	//tester (void);

}


tester::tester (int var1=10, int var2=11, int var3=12, int var4=13): field1(var1), field2(var2), field3(var3), field4(var4)

{

	cout << "Initialized constructor called." << endl;

}

/*

tester::tester(void)

{

	cout << "Default constructor called." << endl;

}

*/


int main () {

	tester ex1;

	cout << "Field 1 = " << ex1.field1 << endl

		<< "Field 2 = " << ex1.field2 << endl

		<< "Field 3 = " << ex1.field3 << endl

		<< "Field 4 = " << ex1.field4 << endl;

	tester ex2 (1,2,3,4);


	cout << "Field 1 = " << ex2.field1 << endl

		<< "Field 2 = " << ex2.field2 << endl

		<< "Field 3 = " << ex2.field3 << endl

		<< "Field 4 = " << ex2.field4 << endl;


	return 0;

}


1>Compiling...

1>main.cpp

1>c:\documents and settings\luke jordan\my documents\visual studio 2008\projects\wg prot 1\wg prot 1\main.cpp(17) : error C2533: 'tester::{ctor}' : constructors not allowed a return type

1>c:\documents and settings\luke jordan\my documents\visual studio 2008\projects\wg prot 1\wg prot 1\main.cpp(29) : error C2264: 'tester::tester' : error in function definition or declaration; function not called

1>c:\documents and settings\luke jordan\my documents\visual studio 2008\projects\wg prot 1\wg prot 1\main.cpp(34) : error C2264: 'tester::tester' : error in function definition or declaration; function not called

The warning messages indicate my constructor has a return type, but it clearly doesn't. I tried changing to void and it returns more errors. Can anyone help me spot the problem?

Thanks,

Luke

#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
class tester
{
public:
	int field1;
	int field2;
	int field3;
	int field4;

	tester (int var1, int var2, int var3, int var4);
	//tester (void);
}[COLOR="Red"][B]; // missing semicolon[/B][/COLOR]


#3
LukeyJ

LukeyJ

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
Haha, so close. Cheers mate.

I'm kicking myself here, 2 mistakes I've made with classes has been a uppercase P for public: and a missing ;. Usually the compiler errors are usually quite clear, this, was not.

Anyway, you restored my sanity so +1 rep heading your way.

static_cast<legend>(dcs);

#4
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
lol. I miss that all the time, so I'm used to seeing a similar message. :P