Advertisement

constructor c++

Started by May 22, 2021 12:44 AM
5 comments, last by Oberon_Command 3 years, 6 months ago

I get an error when I try to use a parametrized constructor, I am a little confused on how to use constructors. Also I have looked up constructors with google.

#include <iostream>
#include <time.h>
#include <math.h>
#include <string>

using namespace std;

class Message
{
public:
	Message();
	Message(string send, string recip) 
	{
	sender=send;
	recipient = recip;
	}
	void append(string message, string append_msg);
	void to_string(string sender, string recipient);
	void print(string message);
private:
	string message;
	string sender;
	string recipient;
	double time;
	string append_msg;
};

void Message::append(string message, string append_msg)
{
	cout << message + append_msg;
}

void Message::to_string(string sender, string recipient)
{
	cout << sender+recipient << endl;
}

void Message::print(string message)
{
	cout << message << endl;
}

int main()
{
	Message msg;
	string sender, recipient, message, append_msg;
	
	cout << "Enter sender: ";
	cin >> sender;
	cout << endl;
	cout << "Enter recipient: ";
	cin >> recipient;
	cout << endl;
	cout << "Enter message: ";
	cin >> message;
	cout << endl;
	cout << "Enter append_msg: ";
	cin >> append_msg;
	cout << endl;

	msg.append(message, append_msg);
	msg.to_string(sender, recipient);
	msg.print(message);

	return 0;
}

You didn't post what error you were getting, but a quick scan of the code reveals that you declared a default constructor, but didn't define it. You also implemented a constructor that takes arguments, but never actually call it. Instead, you're calling the default constructor.

Message msg;
Advertisement

how do you define a default constructor?

Same as the non-default constructors like you do with the 'Message(string send, string recip)' constructor.

Your 'Message(string send, string recip)' constructor has a body (the stuff below it between { and }). Your ‘Message()’ default constructor has no body (no { and } with code to initialize the data members).

As a result, the compiler doesn't know what to do when you create a variable of type Message without specifying a ‘send’ and a ‘recip’. To solve, add a body to the default constructor as well.

If the default constructor should do nothing, you just forgot to add the curly braces to add definition to the declaration:

class Message
{
public:
	Message() {}
	Message(string send, string recip) 
	{
	sender=send;
	recipient = recip;
	}
...

pbivens67 said:

how do you define a default constructor?

Relevant article here.

Short version is, declaring the constructor (what you did) tells the compiler that the symbol exists and what kind of “thing” it is. The constructor's definition is where its implementation lives. Should note, as well, that while again you didn't provide the error you're hitting (please do that in the future, errors are there to help you and us and the error message is important), it seems evident to me that you would actually get a linker error here, not a compiler error, because with that constructor declared the compiler would know how to call the function meaning its definition could be in a different file.

This topic is closed to new replies.

Advertisement