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):
#include "stdafx.h" /*is this needed? It doesn't work otherwise :( */
#include <iostream>
usingnamespace 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.
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
constchar *s2="string literal"; //proper
There's also a second syntax for string literals that instead creates strings of type const wchar_t *: L"άλφα"