hi ,
i'm new in programming world
i want to write c++ class
in c it was ok ..
when i change to c++
i've problems

#include <iostream>
using std::cin;                //  using std::setprecision;
using std::cout;                 using std::string;
using std::endl;               //  using std::streamsize;

class STD1{
	int id;
	string name ;
	char status;
	
	void::pinfo();
	void::ginfo();
};
void STD1::ginfo(){
	cout<<"Enter ID : ";
	cin>>id;
	cout<<"\nEnter Name : ";
	cin>>name;
	cout<<"\nEnter Status : ";
	cin>>status;
}
void STD1::pinfo(){
	cout<<"Student Info Is ... ID : "<<id <<" Name : "<<name <<" Status : "<<status;
}

void main()
{
	STD1 m;
	m.ginfo();
	m.pinfo();
}

the errors i've see is :

1>c:\users\m\documents\visual studio 2008\projects\m\m\class.cpp(29) : error C2248: 'STD1::ginfo' : cannot access private member declared in class 'STD1'
1> c:\users\m\documents\visual studio 2008\projects\m\m\class.cpp(12) : see declaration of 'STD1::ginfo'
1> c:\users\m\documents\visual studio 2008\projects\m\m\class.cpp(6) : see declaration of 'STD1'
1>c:\users\m\documents\visual studio 2008\projects\m\m\class.cpp(30) : error C2248: 'STD1::pinfo' : cannot access private member declared in class 'STD1'
1> c:\users\m\documents\visual studio 2008\projects\m\m\class.cpp(11) : see declaration of 'STD1::pinfo'
1> c:\users\m\documents\visual studio 2008\projects\m\m\class.cpp(6) : see declaration of 'STD1'
1


can any one solve problem and tell me what should i do

i'm waiting ..
thanks

Recommended Answers

All 25 Replies

By default the access specifier is private in a class and hence u are not able to access the ginfo() and pinfo() method of the class STD1.
U have to set them as public.
Do

public:
    void ginfo();
    void pinfo();

ok man thanks .
i make change and aslo remove scope :: from function name
i've now2 errors

the code is

#include <iostream>
using std::cin;                //  using std::setprecision;
using std::cout;                 using std::string;
using std::endl;               //  using std::streamsize;

 class STD1{
	 int id;
	  string name ;
	  char status;
 public :
	 void pinfo();
	 void ginfo();
};
void STD1::ginfo(){
	cout<<"Enter ID : ";
	cin>>id;
	cout<<"\nEnter Name : ";
	cin >> name;
	cout<<"\nEnter Status : ";
	cin>>status;
}
void STD1::pinfo(){
	cout<<"Student Info Is ... ID : "<<id <<" Name : "<<name <<" Status : "<<status;
}

void main()
{
	STD1 m;
	m.ginfo();
	m.pinfo();
}

the error

1>c:\users\m\documents\visual studio 2008\projects\m\m\class.cpp(18) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

can you help me ?

using std::cin;                //  using std::setprecision;
using std::cout;                 using std::string;
using std::endl;               //  using std::streamsize;

make it a one statement as

using namespace std;

I am not sure of why the error is coming because i tested the same in my machine in linux and it was working.

thanks man ..

when i'm write this
string name ;

i've problem
when change it to
char name ;
it's workin and put one char

the problem with string ..!!
any one have an idea
thanks

Why do you want to change a string to a char? A char is ONE character, no more.
Post your newest code, with input and expected output.

but with that u will have only one char so do something like
char name[20];

Thaaaanks bro
thats what i search for ..
i need more than one char caz i want to put name ,, " more one char "
i don't have an idea to declare array
but now with array of char ... its ok

chare name[20];

thanks again ..

Why don't you use a string for input? No need for char-arrays here, if you use std::getline(). Example:

#include <iostream>
#include <string>

using namespace std;

int main(){
    string str = "";
    cout << "enter something: ";
    getline(cin,str);
    cout << "\nyou entered: " << str;
    return 0;
}

[edit]
Also read this.

thanks bro .

bro,
when i put the code you give me
this error appear
1>.\class.cpp(8) : error C2864: 'STD1::name' : only static const integral data members can be initialized within a class

#include <iostream>
#include<string>

using namespace std;

 class STD1{
	 int id;
	 string name="";
	  char status;
 public :
	 void pinfo();
	 void ginfo();
};
void STD1::ginfo(){
	cout<<"Enter ID : ";
	cin>>id;
	cout<<"\nEnter Name : ";
	getline(cin,name);
	cout<<"\nEnter Status : ";
	cin>>status;
}
void STD1::pinfo(){
	cout<<"Student Info Is ... ID : "<<id <<" Name : "<<name <<" Status : "<<status;
}

void main()
{
	STD1 m;
	m.ginfo();
	m.pinfo();
}
string name="";

u can write

string name;

i try this ,,
but the program skip this process
ask me to put the "status" Dirct .

i try this ,,
but the program skip this process
ask me to put the "status" Dirct .

are u getting still more errors. Put them in here then. Will have a look at it.

no error in code .
its run
but the program skip name asking
i mean
Enter id : 1
Enter name :
Enter Status : a
after i put id number ,,, dirct ask for Status

u can write

string name;

Yes you can. But then you have an uninitialized variable in your program. That's a bad thing. That's why I said: string name="";

i use
string name="";
and
string name;

both give me diffrent error ...

Yes you can. But then you have an uninitialized variable in your program. That's a bad thing. That's why I said: string name="";

yes you are right but some compiler do not allow u to initialize a data member of a class like that.
Even I am getting the following error while trying to do so

error: ISO C++ forbids initialization of member `name'

This is the program that worked fine in my compiler(in linux)

#include<iostream>
#include<string>

using namespace std;

 class STD1{
         int id;
         string name;
          char status;
 public :
         void pinfo();
         void ginfo();
};
void STD1::ginfo(){
        cout<<"Enter ID : ";
        cin>>id;
        cout<<"\nEnter Name : ";
        cin>>name;
        cout<<"\nEnter Status : ";
        cin>>status;
}
void STD1::pinfo(){
        cout<<"Student Info Is ... ID : "<<id <<" Name : "<<name <<" Status : "<<status;
}

int main()
{
        STD1 m;
        m.ginfo();
        m.pinfo();
        return 0;
}

when i tried to use getline(cin, name) that statement is skipped. The user is not prompted for any input and goes for the next statement.

thats whats happen with me .

thats whats happen with me .

u mean
cin>>name;
worked in your compiler?

i use
string name="";
and
string name;

both give me diffrent error ...

What about this?......

string name = string("");

That should do the trick!

Cheers for now,
Jas.

What about this?......

string name = string("");

That should do the trick!

Cheers for now,
Jas.

i try that ..
not work ^_^

---

u mean
cin>>name;
worked in your compiler?

Yes ..

i try that ..
not work ^_^

do the initialisation in the default constructor then if u want.

Ah hang on, I see the problem.....I should've spotted this off the bat! heh heh!

#include<iostream>
#include<string>

using namespace std;

 class STD1{
         int id;
         string name;  // you can't initialise it here because this is the technically the header for the class!
          char status;
 public :
         void pinfo();
         void ginfo();
};
void STD1::ginfo(){
        // but you could do it here
        name = string(""); // this should initialise name
        // alternatively, write a constructor for your class 
        // and initialise name there!
        cout<<"Enter ID : ";
        cin>>id;
        cout<<"\nEnter Name : ";
        cin>>name; // now the cin should work!
        cout<<"\nEnter Status : ";
        cin>>status;
}
void STD1::pinfo(){
        cout<<"Student Info Is ... ID : "<<id <<" Name : "<<name <<" Status : "<<status;
}

int main()
{
        STD1 m;
        m.ginfo();
        m.pinfo();
        return 0;
}

I think that might be what you're looking for!
Either initialise it in the ginfo function as shown above or add a constructor to the class and initialise it there!

So using the constructor option:

#include<iostream>
#include<string>

using namespace std;

 class STD1{
         int id;
         string name; 
          char status;
 public :
	 STD1(){name = string("");} // ensure that name is definitely initialised!
         void pinfo();
         void ginfo();
};
void STD1::ginfo(){
        cout<<"Enter ID : ";
        cin>>id;
        cout<<"\nEnter Name : ";
        cin>>name; // now the cin should work!
        cout<<"\nEnter Status : ";
        cin>>status;
}
void STD1::pinfo(){
        cout<<"Student Info Is ... ID : "<<id <<" Name : "<<name <<" Status : "<<status;
}

int main()
{
        STD1 m;
        m.ginfo();
        m.pinfo();
        return 0;
}

I don't see why the cin>>name is failing in the first place though. The initial code you posted works fine for me in VS2003.

At the moment your class uses the default constructor..perhaps that is failing to initialise the name member variable correctly causing your cin>>name to fail!
Either way, if that is the case then initialising name in one of the already mentioned ways should get around the problem and your cin>>name call should work fine!!

Cheers for now,
Jas.

hmmmm
thanks aloot man
thanks

write a menu driven program which has following option 1.factorial 2.prime OR not -3.odd OR even 4.exit .make a use of switch case statement.use of a function for factorial,prime& odd/even calculation

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.