String problems

Well I have made this program with a class that when called with the setMessage() it will set the message then when getMessage() is called it should display the message but I just get compiler errors.

Code for Main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "Message.h"
#include <iostream>
#include <string>


using namespace std;

int main()
{
	Message msg();
	msg.setMessage();
	msg.getMessage();
	return 0;
}


code for Message.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string.h>


class Message
{
public:
	Message(std::string initialMessage);
	~Message();
	std::string getMessage() { return message; }
	void setMessage(std::string message);
private:
	std::string message;
}

Message::Message(std::string initialMessage)
{
	setMessage(initialMessage);
}
Message::~Message() {}

void Message::setMessage(std::string newMessage)
{
	std::cout << "Please enter a message:\n";
	std::cin >> newMessage;
	message = newMessage;
}
You don't seem to understand function parameters, so you should look that up again.
Explain. Please
setMessage takes one parameter, but you're not passing any arguments when calling the function in main(). The same applies to the Message constructor.

Message msg(); parses as a function declaration, by the way.
Write Message msg; instead (if your class had a default constructor).
Last edited on
void Message::setMessage(std::string newMessage)

This means when you call this function, it is expecting a string to be passed. An example would be,

messageObj.setMessage(myString);
Thanks! yea just a bit tired and cant think straight.
Topic archived. No new replies allowed.