对于看起来非常好的C++代码,VC++出错?

Laz*_*Laz 1 c++ visual-c++

大家好.看看这段示例代码.

#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;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

1> ------ Build build:项目:首先,配置:调试Win32 ------ 1> first.cpp 1> c:\ documents and settings\dark wraith\my documents\visual studio 2010\projects\first\first\first.cpp(9):错误C2144:语法错误:'char'前面应加':'

1> c:\ documents and settings\dark wraith\my documents\visual studio 2010\projects\first\first\first.cpp(10):error C2144:语法错误:'int'前面应加':'

1> c:\ documents and settings\dark wraith\my documents\visual studio 2010\projects\first\first\first.cpp(12):error C2144:语法错误:'void'前面应加':'

1> c:\ documents and settings\dark wraith\my documents\visual studio 2010\projects\first\first\first.cpp(17):error C2144:语法错误:'void'前面应加':'== ========构建:0成功,1失败,0最新,0跳过==========

Nav*_*een 11

声明的语法publicprivate是错误的.与其他语言不同,在C++中它应该是

class person{
private: 
char name[20];
 int age;
public:
  void display();
Run Code Online (Sandbox Code Playgroud)

....

  • @Ram:你写的东西对我来说似乎是C和C#的地狱混合,伤害了我的眼睛.也许这适用于C++/CLI?我不知道,因为我从未使用它. (3认同)