1

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

2
  • 3
    That code looks very C'ish to me. May 19, 2010 at 7:00
  • i wonder how you define "perfectly good c++ code" ?
    – YeenFei
    May 20, 2010 at 8:48

2 Answers 2

11

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

....

3
  • thanks Naveen and Alex. But i remember writing code where i specified each fields access specifiers. Is this only specific to MSVC++?
    – Laz
    May 19, 2010 at 6:57
  • 3
    @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
    May 19, 2010 at 7:02
  • public void function() is the Java way I believe.
    – Mark B
    May 19, 2010 at 14:48
3

In C++, private works like this:

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

Note the colon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.