Error in VC++ for code that looks perfectly good C++?
Hey guys. Check out this piece of sample code.
#include "stdafx.h"
#include<conio.h>
#include<string.h>
class person{
private char name[20];
private int age;
public void setValu开发者_运维问答es(char n[],int a)
{
strcpy(this->name,n);
this->age=a;
}
public void display()
{
printf("\nName = %s",name);
printf("\nAge = %d",age);
}
};
int _tmain(int argc, _TCHAR* argv[])
{
person p;
p.setValues("ram",20);
p.display();
getch();
return 0;
}
I am getting the following errors :
1>------ Build started: Project: first, Configuration: Debug Win32 ------ 1> first.cpp 1>c:\documents and settings\dark wraith\my documents\visual studio 2010\projects\first\first\first.cpp(9): error C2144: syntax error : 'char' should be preceded by ':'
1>c:\documents and settings\dark wraith\my documents\visual studio 2010\projects\first\first\first.cpp(10): error C2144: syntax error : 'int' should be preceded by ':'
1>c:\documents and settings\dark wraith\my documents\visual studio 2010\projects\first\first\first.cpp(12): error C2144: syntax error : 'void' should be preceded by ':'
1>c:\documents and settings\dark wraith\my documents\visual studio 2010\projects\first\first\first.cpp(17): error C2144: syntax error : 'void' should be preceded by ':' ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The syntax of declaring public
and private
is wrong. Unlike other languages, in C++ it should be
class person{
private:
char name[20];
int age;
public:
void display();
....
In C++, private
works like this:
class A
{
private:
void f();
void g();
};
Note the colon.
精彩评论