syntax error : 'constant'

S

Someonekicked

this is example of what is going on my program, and the error im getting :

when i try to compile the following :

#include <iostream>
using namespace std;

class first {
public:
first(int max) { };
};

class whatever {
public:
whatever() { };
private:
first sowhat(2);
};



I get the error, syntax error : 'constant'
any ideas to solve it?
 
V

Victor Bazarov

Someonekicked said:
this is example of what is going on my program, and the error im getting :

when i try to compile the following :

#include <iostream>
using namespace std;

class first {
public:
first(int max) { };

Lose the extraneous semicolon.
};

class whatever {
public:
whatever() { };


Lose the extraneous semicolon.
private:
first sowhat(2);

What are you trying to accomplish? Are you trying to define a member
and initialise it here? You can't. Initialisation is done in the
constructor. Now, you wrote a default constructor and it does nothing
in your case. Why do you think you need it, then?
};



I get the error, syntax error : 'constant'
any ideas to solve it?

Yes. How about your book, doesn't it tell you how to initialise members
in the constructor?

class whatever { ...
whatever() : sowhat(2) {}
...
};

Recently there were such basic questions here I am wondering whether
some folks lost the ability to absorb necessary knowledge from books
altogether. Present company excluded, of course...

V
 
M

Mike Wahler

Someonekicked said:
this is example of what is going on my program, and the error im getting :

when i try to compile the following :

#include <iostream>
using namespace std;

class first {
public:
first(int max) { };
};

class whatever {
public:
whatever() { };
private:
first sowhat(2);
};



I get the error, syntax error : 'constant'
any ideas to solve it?

#include <iostream>

class first
{
public:
first(int max) : m (max) { };
int value() const { return m; }

private:
int m;
};

class whatever
{
public:
whatever(int arg) : sowhat(arg) { };
int f() const { return sowhat.value(); }
private:
first sowhat;
};

int main()
{
whatever w(2);
std::cout << w.f() << '\n'; /* prints 2 */
return 0;
}

-Mike
 

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
474,184
Messages
2,570,978
Members
47,578
Latest member
LC_06

Latest Threads

Top