tags:

views:

103

answers:

2

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>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 ==========

+8  A: 

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();

....

Naveen
thanks Naveen and Alex. But i remember writing code where i specified each fields access specifiers. Is this only specific to MSVC++?
Ram Bhat
no this is standard C++, you might have done it in C#
Naveen
@Ram: What you wrote seems like a hellish mix of C and C# to me and hurts my eyes. Maybe this works in C++/CLI? I wouldn't know, since I never used it.
sbi
`public void function()` is the Java way I believe.
Mark B
+3  A: 

In C++, private works like this:

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

Note the colon.

Alex - Aotea Studios