#include "stdafx.h"
#include<conio.h>
#include<string.h>
class person{
private char name[20];
private int age;
public void setValues(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;
}
我收到以下错误:
1&gt; ------ Build build:项目:首先,配置:调试Win32 ------ 1 GT; first.cpp 1&gt; c:\ documents and settings \ dark wraith \ my documents \ visual studio 2010 \ projects \ first \ first \ first.cpp(9):error C2144:语法错误:'char'前面应加':'
1&gt; c:\ documents and settings \ dark wraith \ my documents \ visual studio 2010 \ projects \ first \ first \ first.cpp(10):error C2144:语法错误:'int'前面应加': “
1&gt; c:\ documents and settings \ dark wraith \ my documents \ visual studio 2010 \ projects \ first \ first \ first.cpp(12):error C2144:语法错误:'void'前面应加': “
1&gt; c:\ documents and settings \ dark wraith \ my documents \ visual studio 2010 \ projects \ first \ first \ first.cpp(17):error C2144:语法错误:'void'前面应加': “ ==========构建:0成功,1个失败,0个最新,0个跳过==========
答案 0 :(得分:11)
声明public
和private
的语法错误。与其他语言不同,在C ++中它应该是
class person{
private:
char name[20];
int age;
public:
void display();
...
答案 1 :(得分:3)
在C ++中,private
的工作原理如下:
class A
{
private:
void f();
void g();
};
请注意冒号。