473,803 Members | 3,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's wrong with the following program?

Hi,

It seems that the line with "//no error" and "//error" are the same.
However, the second one give an error.

I'm using g++-3.3.
The error message was:

g++-3.3 -g -c -o main.o main.cc
main.cc: In function `int main(int, char**)':
main.cc:85: error: parse error before `,' token
make: *** [main.o] Error 1

Best wishes,
Peng
template <typename __Tp>
class A{
public:
A(){};
~A(){};
virtual void fun() = 0;
};

template <typename __Tp>
class A1 : public A<__Tp>{
public:
A1(){};
~A1(){};
virtual void fun() {};
};

template <typename __Tp>
class B{
public:
B(__Tp x, __Tp y, __Tp z){};
~B(){};
virtual void fun() = 0;
};

template <typename __Tp>
class B1 : public B<__Tp>{
public:
B1(__Tp x, __Tp y): B<__Tp>(x, x, y){};
~B1(){};
virtual void fun() {};
};
template <typename __Tp>
class D{
public:
D(const A<__Tp> &A_inst, const B<__Tp> &B_inst){};
~D(){};
};

int main(int argc, char *argv[])
{
const double x = 0;
A1<double> A1_inst;
B1<double> B1_inst(x, x);
//D<double> D_inst(A1_inst, B1_inst);//no error
D<double> D_inst(A1<doubl e>, B1<double>(x,x) );//error
}

Jul 23 '05 #1
4 1541
Pe*******@gmail .com wrote:
Hi,

....
int main(int argc, char *argv[])
{
const double x = 0;
A1<double> A1_inst;
B1<double> B1_inst(x, x);
//D<double> D_inst(A1_inst, B1_inst);//no error
D<double> D_inst(A1<doubl e>, B1<double>(x,x) );//error
}


This is being interpreted as a declaration of a function named D_inst.

Try this:
D<double> D_inst(A1<doubl e>(), B1<double>(x,x) );

or this:
D<double> D_inst = D<double>(A1<do uble>(), B1<double>(x,x) );
Jul 23 '05 #2
Pe*******@gmail .com wrote:
It seems that the line with "//no error" and "//error" are the same.
However, the second one give an error.

I'm using g++-3.3. cat main.cpp template <typename __Tp>
class A {
public:
A(void) { };
~A(void) { };
virtual void fun(void) = 0;
};

template <typename __Tp>
class A1: public A<__Tp> {
public:
A1(void) { };
~A1(void) { };
virtual void fun(void) { };
};

template <typename __Tp>
class B {
public:
B(__Tp x, __Tp y, __Tp z) { };
~B(void) { };
virtual void fun(void) = 0;
};

template <typename __Tp>
class B1: public B<__Tp> {
public:
B1(__Tp x, __Tp y): B<__Tp>(x, x, y) { };
~B1(void) { };
virtual void fun(void) {};
};
template <typename __Tp>
class D {
public:
D(const A<__Tp> &A_inst, const B<__Tp> &B_inst) { };
~D(void) { };
};

int main(int argc, char *argv[]) {
const double x = 0;
A1<double> A1_inst;
B1<double> B1_inst(x, x);
//D<double> D_inst(A1_inst, B1_inst);//no error
D<double> D_inst(A1<doubl e>(), B1<double>(x, x));//error
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cpp main.cpp: In instantiation of `A<double>':
main.cpp:10: instantiated from `A1<double>'
main.cpp:43: instantiated from here
main.cpp:2: warning: `class A<double>' \
has virtual functions but non-virtual destructor
main.cpp: In instantiation of `A1<double>':
main.cpp:43: instantiated from here
main.cpp:10: warning: `class A1<double>' \
has virtual functions but non-virtual destructor
main.cpp: In instantiation of `B<double>':
main.cpp:26: instantiated from `B1<double>'
main.cpp:44: instantiated from here
main.cpp:18: warning: `class B<double>' \
has virtual functions but non-virtual destructor
main.cpp: In instantiation of `B1<double>':
main.cpp:44: instantiated from here
main.cpp:26: warning: `class B1<double>' \
has virtual functions but non-virtual destructor
g++ --version | head -1

g++ (GCC) 3.4.1
Jul 23 '05 #3
Gianni Mariani wrote:
Pe*******@gmail .com wrote:
Hi,

...
int main(int argc, char *argv[])
{
const double x = 0;
A1<double> A1_inst;
B1<double> B1_inst(x, x);
//D<double> D_inst(A1_inst, B1_inst);//no error
D<double> D_inst(A1<doubl e>, B1<double>(x,x) );//error
}


This is being interpreted as a declaration of a function named D_inst.

Try this:
D<double> D_inst(A1<doubl e>(), B1<double>(x,x) );


Using "g++ (GCC) 3.3.4 (pre 3.3.5 20040809)"
the above line produces this error:

"main.cpp:4 7: error: syntax error before `,' token"

Why?

or this:
D<double> D_inst = D<double>(A1<do uble>(), B1<double>(x,x) );


This 2nd solution works. It's creating a temp D<double> then
copying it to D_inst. Why should that work, but your first
solution not work? You first solution calls the constructor
for A<double> directly, and looks perfectly legal.

Regards,
Larry
--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #4
Larry I Smith wrote:
Gianni Mariani wrote:
Pe*******@gmail .com wrote:
Hi,


...
int main(int argc, char *argv[])
{
const double x = 0;
A1<double> A1_inst;
B1<double> B1_inst(x, x);
//D<double> D_inst(A1_inst, B1_inst);//no error
D<double> D_inst(A1<doubl e>, B1<double>(x,x) );//error
}


This is being interpreted as a declaration of a function named
D_inst.

Try this:
D<double> D_inst(A1<doubl e>(), B1<double>(x,x) );


Using "g++ (GCC) 3.3.4 (pre 3.3.5 20040809)"
the above line produces this error:

"main.cpp:4 7: error: syntax error before `,' token"

Why?


Adding the empty parentheses hasn't changed anything; the compiler will
still parse it incorrectly. Try putting parentheses around the
temporary object:

D<double> D_inst((A1<doub le>()), B1<double>(x,x) );

Incidentally, this is an instance of "C++'s Most Vexing Parse" from
Scott Meyers' book _Effective_STL_ .

Kristo

Jul 23 '05 #5

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

Similar topics

125
14865
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
56
4347
by: Cherrish Vaidiyan | last post by:
Frinds, Hope everyone is doing fine.i feel pointers to be the most toughest part in C. i have just completed learning pointers & arrays related portions. I need to attend technical interview on C. wat type of questions should be expected? Which part of C language do the staff give more concern? The interviewers have just mentioned that .. i will have interview on C. Also can anyone can help me with sites where i can go thru sample
51
4573
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is there something wrong in there? -------------------------------------------------------------------- Types A type is a definition for a sequence of storage bits. It gives the meaning of the data stored in memory. If we say that the object a is an
6
5768
by: alternativa | last post by:
Hi, I have problem with the following function - it was intended to ask a user for a 4-digits number: double ask_for_number (void) { char *notint; char s2; double entered_number;
18
3137
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is how global variables and function returns work... For example, I have a global array "char datestring;" which is defined in the function speakdate. speakdate just converts a set of integers (date variables) to a string.
22
2297
by: noridotjabi | last post by:
Okay. I'm quite embarased to be asking this but I cannot seem to get files to work. I don't know what the problem is. Is there something wrong with this: (file related) #include <stdio.h> main() { FILE *pfsin; pfsin = fopen("msm.pfs", "r"); char mystr;
669
26255
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
5
10815
by: Ryan | last post by:
{"POINTID":77902,"MAPID":762,"LONG":-122.21654892,"LAT":"37.1834331019","CITY":"Boulder Creek","STATE":"CA","DIST":5745.4} I get an "invalid label" error... I'm kinda new to this. Thanks!
7
3662
by: Sunil Korah | last post by:
Hi, I haven't used access reports much. I have a problem in getting the total of a group. I have 3 fields, ProgName (Program name), Pname (Participant's name) and PCategory (Participant category) PCategory can have values 'SC', 'ST' and 'Others'
16
1789
by: chutsu | last post by:
Ok Here is a problem, I got a imaginary database program that I need to code, to add a patient I have function inser_patient. but when I try to input the details it doesn't quite work the way I wanted it to. Code: #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h>
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9564
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10548
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10316
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.