Error: Too many characters in constant?

Hello CPlusPlus Community,

I'm pretty new to C++. I have a book ('C++ in 21 days', it takes me longer :P) and I'm now learning to make classes, public/private, constructors/destructors, that kind of stuff. I use Visual C++ 2008 Express Edition.

I get an error on the following piece of code (nothing spectacular, just to try some things out):
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
#include "stdafx.h" /*is this needed? It doesn't work otherwise :( */
#include <iostream>
using namespace std;

class Cat //the Class
{
public:
	int getAge();
	void setAge(int age);
	void makeSound();
private:
	int herAge;
};

int Cat::getAge() //get the age
{
	return herAge;
}

void Cat::setAge(int age) //set the age
{
	herAge = age;
}

void Cat::makeSound() //print 'Miauw!'
{
	cout << 'Miauw!\n';
}

int main()
{

	Cat Frisky; //Frisky is a Cat
	Frisky.setAge(6); //Her age is 6
	Frisky.makeSound(); //print Miauw!
	cout << 'Frisky is ' << Frisky.getAge() << ' years of age!\n'; //get the age
	Frisky.makeSound(); //print Miauw!
	
	return 0;

}



The error code I get is the following:
1
2
3
blahblahblah/standard file.cpp(30) : error C2015: too many characters in constant
blahblahblah/standard file.cpp(39) : error C2015: too many characters in constant
blahblahblah/standard file.cpp(39) : error C2015: too many characters in constant


I tried some things but I can't find the error, I hope one of you can help me, cause I wanna move on. I'm stuck here for some time now :P.

-Delpee

PS: is this in the right section?
Last edited on
You're trying to print one character in the cout when you really want to print a string.

Replace your ' with " in line 27 and 36
Last edited on
I'm so stupid! :O
Well, thanks! :D

So ' is for when you want to print one character, but you use " when you want to print multiple?
Single quotes (a different way to call the apostrophe) are used to get the integer representation of a single character. For example, in a system with ASCII as its code page, 'A'==65 is true. There's a second syntax used for wide characters: L'β'

Double quotes are used for string literals. String literals are allocated in the executable and loaded at run time to read-only memory. Their type is const char *, but can be implicitly converted to char *. This is for compatibility with C.
1
2
char *s="string literal"; //valid but improper
const char *s2="string literal"; //proper 

There's also a second syntax for string literals that instead creates strings of type const wchar_t *: L"άλφα"
Thanks for the further explanation Helios!
I understand my mistake now ;-).
Std::string is an object :l
chrisname: What's your point?
Topic archived. No new replies allowed.