简体   繁体   English

VC ++中看起来完美的C ++代码出错?

[英]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 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;
}

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> ------开始构建:项目:首先,配置:调试Win32 ------ 1> first.cpp 1> c:\\ documents and settings \\ dark wraith \\ mydocuments \\ 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: syntax error : 'int' should be preceded by ':' 1> c:\\ documents and settings \\深色幽灵\\我的文档\\ Visual Studio 2010 \\ projects \\ first \\ first \\ first.cpp(10):错误C2144:语法错误:'int'之前应带有':'

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 \\深色幽灵\\我的文档\\ Visual Studio 2010 \\ projects \\ first \\ first \\ first.cpp(12):错误C2144:语法错误:'void'之前应加':'

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 ========== 1> c:\\ documents and settings \\ dark wraith \\我的文档\\ Visual Studio 2010 \\ projects \\ first \\ first \\ first.cpp(17):错误C2144:语法错误:'void'前应加':'== ========构建:0成功,1失败,0最新,跳过0 ==========

The syntax of declaring public and private is wrong. 声明publicprivate的语法是错误的。 Unlike other languages, in C++ it should be 与其他语言不同,在C ++中

class person{
private: 
char name[20];
 int age;
public:
  void display();

.... ....

In C++, private works like this: 在C ++中, private作品如下:

class A 
{
private:
    void f();
    void g();
};

Note the colon. 注意冒号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM