how to declare structure pointer in within a class

S

sharat

hi all

can any body tell how to declare the structure pointer within a class.

i have written the following program but gettin error.

#include<iostream>

using namespace std;

class A
{

public :
struct sample
{
int a ;
int b ;
};
struct sample var1;
struct sample *ptr = &var1;
};

int main()
{

cout<<"\n struct variable ptr prog\n";
}

i m using fc4,gcc compiler.
i m getting the following error.struct.cpp:15: error: 'a::var1'
cannot appear in a constant-expression

struct.cpp:15: error: `&' cannot appear in a constant-expression
struct.cpp:15: error: ISO C++ forbids initialization of member
'ptr'
struct.cpp:15: error: making 'ptr' static
struct.cpp:15: error: invalid in-class initialization of static data
member of non-integral type 'a::sample*'

can any body tell the solution.
thanx in advance.
 
S

sam_cit

class A
{

public :
struct sample
{
int a ;
int b ;
};
struct sample var1;
struct sample *ptr = &var1;
};

You can initialize in a constructor, basically the executable
statement should be under some function.
 
G

Gianni Mariani

sharat wrote:
....
class A
{

public :
struct sample
{
int a ;
int b ;
};
struct sample var1;
struct sample *ptr = &var1;
};
....

You initialize members in the constructor... like so.

class A
{
public :
struct sample
{
int a ;
int b ;
};
sample var1;
sample *ptr;

A()
: var1(),
ptr( & var1 )
{}
};
 
U

Uenal S. Mutlu

Gianni said:
sharat wrote:
...


...

You initialize members in the constructor... like so.

class A
{
public :
struct sample
{
int a ;
int b ;
};
sample var1;
sample *ptr;

A()
: var1(),
ptr( & var1 )
{}
};

What a load a crap.
 
F

frame

Uenal said:
What a load a crap.

Hey! don't break wind???...
[NOTE: As long as you post this kind of stuff in this group, I shall
follow up with this advice!]
 
J

John Carson

frame said:
Uenal said:
What a load a crap.

Hey! don't break wind???...
[NOTE: As long as you post this kind of stuff in this group, I shall
follow up with this advice!]


And then the rest of us will have to read it. Don't feed the trolls. Add
them to your blocked senders list.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top