About stringstream

I have 2 errors in this piece of code, namely "Error: incomplete type is not allowed" for the "info" words and "Error: no operator "<<" matches these operands" for the "<<". I have no idea what I've done wrong. It worked for one of my other programs. I've included iostream and sstream.

Would appreciate any help!

1
2
3
4
5
6
7
8
9
10
  string City::toString()
{
	stringstream info;

	info<<"City name: "<<cityName<<endl;
	info<<"City size: "<<citySize<<endl;
	info<<"City postal code: "<<cityCode<<endl;

	return info.str();
}
this compiles
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
string toString()
{
	stringstream info;
	string cityName, citySize, cityCode;

	info<<"City name: "<<cityName<<endl;
	info<<"City size: "<<citySize<<endl;
	info<<"City postal code: "<<cityCode<<endl;

	return info.str();
}


provide enough code to reproduce your problem.
The code belongs to the following cpp file:

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "City.h"
#include<string>
#include<sstream>
#include<iostream>
using namespace std;

City::City(string n,string s,int c)
{
	cityName=n;
	citySize=s;
	cityCode=c;
}

string City::getCityName()
{
	return cityName;
}

void City::setCityName(string name)
{
	cityName=name;
}

string City::getCitySize()
{
	return citySize;
}

void City::setCitySize(string size)
{
	citySize=size;
}

int City::getCityCode()
{
	return cityCode;
}

void City::setCityCode(int code)
{
	cityCode=code;
}

string City::toString()
{
	stringstream info;

	info<<"City name: "<<cityName<<endl;
	info<<"City size: "<<citySize<<endl;
	info<<"City postal code: "<<cityCode<<endl;

	return info.str();
}


The header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once
#include<string>
using namespace std;
class City
{
private:
	string cityName;
	string citySize;
	int cityCode;
public:
	City(string n,string s,int c);
	string toString();
	string getCityName();
	void setCityName(string name);
	string getCitySize();
	void setCitySize(string size);
	int getCityCode();
	void setCityCode(int code);
}
You're missing a ; on line 19 of your header file.
Thank you so much!!!

I've been looking at my code for hours trying to find out what's wrong!

Thank you! I can finally proceed with the rest of my code.
Topic archived. No new replies allowed.