473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Help: constructor syntax

I get a compile time error that I don't understand.

======================================= begin constructor.cc ======
struct Negate {};

class A {
int i;
public:
A(double x ): i(int(x)) {}; // 0
A(Negate ): i(-1 ) {}; // 1
A(double, int ii): i(4*ii ) {}; // 2
A(Negate, int ii): i( -ii ) {}; // 3
};

void foob(Negate n, int x) {
A a(n,x);
};

template <typename T>

void f() {
A a0(3.0);
A a1(Negate());
A a2(1.4 , 3);
A a3(Negate(), 6); // error
Negate dummy;
A a4(dummy , 6);
A a5 = A(Negate(), 6);
foob(Negate(), 6);
}
======================================= end constructor.cc ======

This gives me two errors on the indicated line. I intend to initialize a3
directly with the third constructor, just like a4 and a (and a5?).

======================================= begin compiler output ======
make -k constructor.o
g++ -c -o constructor.o constructor.cc
constructor.cc: In function `void f()':
constructor.cc:22: type specifier omitted for parameter
constructor.cc:22: parse error before numeric constant
make: *** [constructor.o] Error 1
======================================= end compiler output ======

I'd prefer not to use a dummy object. What is the correct syntax? And what does
the error try to tell me?

Thanks

Christian

Jul 19 '05 #1
8 3539

"Christian Brechbühler" <c_************@yhaoo.com> wrote in message
news:kBhob.48975$275.119519@attbi_s53...
I get a compile time error that I don't understand.

======================================= begin constructor.cc ======
struct Negate {};

class A {
int i;
public:
A(double x ): i(int(x)) {}; // 0
A(Negate ): i(-1 ) {}; // 1
A(double, int ii): i(4*ii ) {}; // 2
A(Negate, int ii): i( -ii ) {}; // 3
};

void foob(Negate n, int x) {
A a(n,x);
};

template <typename T>

void f() {
A a0(3.0);
A a1(Negate());
A a2(1.4 , 3);
A a3(Negate(), 6); // error
Negate dummy;
A a4(dummy , 6);
A a5 = A(Negate(), 6);
foob(Negate(), 6);
}
======================================= end constructor.cc ======

This gives me two errors on the indicated line. I intend to initialize a3
directly with the third constructor, just like a4 and a (and a5?).

======================================= begin compiler output ======
make -k constructor.o
g++ -c -o constructor.o constructor.cc
constructor.cc: In function `void f()':
constructor.cc:22: type specifier omitted for parameter
constructor.cc:22: parse error before numeric constant
make: *** [constructor.o] Error 1
======================================= end compiler output ======

I'd prefer not to use a dummy object. What is the correct syntax? And what does the error try to tell me?

Thanks


I made a quick glance through your code and the most apparent mistake is that
there should be no semicolon after function definitions.
i.e. A(double x ): i(int(x)) {}; // 0
should be A(double x ): i(int(x)) {} //No semicolon after braces.

Try that out.

HTH,
J.Schafer

Jul 19 '05 #2

"Josephine Schafer" <no****************@hotmail.com> wrote in message
news:bn*************@ID-192448.news.uni-berlin.de...

"Christian Brechbühler" <c_************@yhaoo.com> wrote in message
news:kBhob.48975$275.119519@attbi_s53...
I get a compile time error that I don't understand.
[SNIP] I'd prefer not to use a dummy object. What is the correct syntax? And what
does
the error try to tell me?

Thanks
I made a quick glance through your code and the most apparent mistake is

that there should be no semicolon after function definitions.
i.e. A(double x ): i(int(x)) {}; // 0
should be A(double x ): i(int(x)) {} //No semicolon after braces.

Try that out.

HTH,
J.Schafer


As long as the implementation is done directly inside the class where the
functions are defined, then the extra semicolons do no harm.
void foob(Negate n, int x) {
A a(n,x);
};


But this piece of code is troublesome 'cause here the OP should skip the
trailing semicolon after the end of scope brace. Although some compilers
(MSVC for example) will dump the semicolon silently.

To the OP:

Instead of using a dummy struct, why don't you use a enumerated flag to
indicate the operation. For example:

class A {
int i;
public:
enum Operation { Negate, Multiply };
A(double x ): i(int(x)) {}; // 0
A( ): i(-1 ) {}; // 1
A(double, int ii): i(4*ii ) {}; // 2
A( Operation, int ii): i( -ii ) {}; // 3
};

void foob( A::Operation n, int x) {
A a(n,x);
}

template <typename T>
void f() {
A a0(3.0);
A a1( A::Negate );
A a2(1.4 , 3);
A a3( A::Negate, 6); // error
A a5 = A( A::Negate , 6);
foob( A::Negate, 6);
}

HTH
Chris
Jul 19 '05 #3


Christian Brechbühler wrote:


I'd prefer not to use a dummy object. What is the correct syntax? And what does
the error try to tell me?


I am not sure why the compiler emits that errors, It looks ok to me.
But then you should get into the habit of accepting class objects per
const reference instead of per value.

Try:

class A {
int i;
public:
A(double x ): i(int(x)) {}; // 0
A( const Negate & ): i(-1 ) {}; // 1
A(double, int ii): i(4*ii ) {}; // 2
A(const Negate &, int ii): i( -ii ) {}; // 3
};
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #4

"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message
news:bn**********@sunnews.cern.ch...

"Josephine Schafer" <no****************@hotmail.com> wrote in message
news:bn*************@ID-192448.news.uni-berlin.de...

"Christian Brechbühler" <c_************@yhaoo.com> wrote in message
news:kBhob.48975$275.119519@attbi_s53...
I get a compile time error that I don't understand.
[SNIP] I'd prefer not to use a dummy object. What is the correct syntax? And what
does
the error try to tell me?

Thanks


I made a quick glance through your code and the most apparent mistake is

that
there should be no semicolon after function definitions.
i.e. A(double x ): i(int(x)) {}; // 0
should be A(double x ): i(int(x)) {} //No semicolon after braces.

Try that out.

HTH,
J.Schafer


As long as the implementation is done directly inside the class where the
functions are defined, then the extra semicolons do no harm.

True..my bad .
void foob(Negate n, int x) {
A a(n,x);
};


But this piece of code is troublesome 'cause here the OP should skip the
trailing semicolon after the end of scope brace. Although some compilers
(MSVC for example) will dump the semicolon silently.

yes..this ofcourse is still an error.
Jul 19 '05 #5
Chris Theis wrote:
"Josephine Schafer" <no****************@hotmail.com> wrote in message
news:bn*************@ID-192448.news.uni-berlin.de...
"Christian Brechbühler" <c_************@yhaoo.com> wrote in message
news:kBhob.48975$275.119519@attbi_s53...
I get a compile time error that I don't understand.
I made a quick glance through your code and the most apparent mistake is

that there should be no semicolon after function definitions.

As long as the implementation is done directly inside the class where the
functions are defined, then the extra semicolons do no harm.


I confirm that dropping the semicolons makes no difference
void foob(Negate n, int x) {
A a(n,x);
};


But this piece of code is troublesome 'cause here the OP should skip the
trailing semicolon after the end of scope brace. Although some compilers
(MSVC for example) will dump the semicolon silently.


No difference here either (gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)).
To the OP:

Instead of using a dummy struct, why don't you use a enumerated flag to
indicate the operation. For example:

class A {
int i;
public:
enum Operation { Negate, Multiply };
A(double x ): i(int(x)) {}; // 0
A( ): i(-1 ) {}; // 1
A(double, int ii): i(4*ii ) {}; // 2
A( Operation, int ii): i( -ii ) {}; // 3
};

void foob( A::Operation n, int x) {
A a(n,x);
}

template <typename T>
void f() {
A a0(3.0);
A a1( A::Negate );
A a2(1.4 , 3);
A a3( A::Negate, 6); // error
A a5 = A( A::Negate , 6);
foob( A::Negate, 6);
}


Yes, this works, thanks! I'm going to use this solution. To answer "why not an
enum" -- in case I wanted the Multiply, I would put the factor in as a member.

I gather that the error with A(Negate(), 6) is spurious, i.e., may be a compiler bug?

Thanks again

Christian

Jul 19 '05 #6
Karl Heinz Buchegger wrote:

Christian Brechbühler wrote:

I'd prefer not to use a dummy object. What is the correct syntax? And what does
the error try to tell me?

I am not sure why the compiler emits that errors, It looks ok to me.
But then you should get into the habit of accepting class objects per
const reference instead of per value.

Try:

class A {
int i;
public:
A(double x ): i(int(x)) {}; // 0
A( const Negate & ): i(-1 ) {}; // 1
A(double, int ii): i(4*ii ) {}; // 2
A(const Negate &, int ii): i( -ii ) {}; // 3
};

Thanks for confirming it looks right. Maybe it's a compiler bug.

I am in the habit of passing objects by reference. In this particular case
however, sizeof(Negate) = 0, and the size of the reference is 4 bytes. So I chose
the smaller, passing by value.
For the syntax sake, I changed my code using your suggestion:

struct Negate {};

class A {
int i;
public:
A(int ii) : i( ii) {}
A(const Negate&, int ii): i(-ii) {}
};

void f() {
A a0(3);
A a3(Negate(), 6); // error
}

Unfortunately, the error is unchanged. The closest to an "no-operation" operator
I found in C++ is *& -- as in, A a3(*&Negate(), 6). That gets rid of the error,
but gives me a warning for taking the address of a temporary.

I'll go with the enum solution proposed by Karl-Heinz Buchegger.

Thanks again

Christian

Jul 19 '05 #7

"Christian Brechbühler" <c_************@yhaoo.com> wrote in message
news:Ghuob.55473$ao4.150884@attbi_s51...

Thanks for confirming it looks right. Maybe it's a compiler bug.

I am in the habit of passing objects by reference. In this particular case however, sizeof(Negate) = 0, and the size of the reference is 4 bytes. So I chose the smaller, passing by value.


OK, but sizeof(Negate) should not be 0. Have you actually checked it?
Jul 19 '05 #8
jeffc wrote:
"Christian Brechbühler" <c_************@yhaoo.com> wrote in message
news:Ghuob.55473$ao4.150884@attbi_s51...
Thanks for confirming it looks right. Maybe it's a compiler bug.

I am in the habit of passing objects by reference. In this particular


case
however, sizeof(Negate) = 0, and the size of the reference is 4 bytes. So


I chose
the smaller, passing by value.

OK, but sizeof(Negate) should not be 0. Have you actually checked it?


No, I hadn't. I assumed, because normally, a plain class/struct's size is the sum
of the data members, except for alignment.

You are absolutely right. I checked, and the size is 1 (still less than 4).

Thanks for catching this!

Christian

Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: J. Campbell | last post by:
I'm comfortable with arrays from previous programming, and understand the advantages of c++ vectors...I just don't understand how to use them :~( Can you help me to use a vector<string> in the...
34
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is...
15
by: PhilB | last post by:
Hello experts, I am a complete beginner in C++ (although I know C). I am trying to compile the code below, and I get the following error. Can anyone explain to me my mistake? Thanks! PhilB ...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
4
by: Dan Stromberg | last post by:
Hi folks. I'm working on building some software, some of which is written in C++, for a researcher here at the University. I have an extensive background in C and python, but I haven't done...
6
by: daveb | last post by:
I'm trying to write some code that calls the constructors of STL containers explicitly, and I can't get it to compile. A sample program is below. One compiler complains about the last two lines...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.