Click here to Skip to main content
11,705,753 members (55,819 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Linux
class A {
   public:
      virtual ~A() {};
      virtual int emit(int t, void* a)=0;
};
 
template <class dest_type="">
class B : public A {
   public:
      typedef int (dest_type::*classpfn)(int, void*);
      B(dest_type* pclass, classpfn pfn) : p(pclass), fn(pfn) {};
      virtual int emit(int t, void* a) { return (p->*fn)(t, a); }
   private:
      dest_type *p;
      classpfn fn;
};
Output:
=======
abc.h:222: error: expected unqualified-id before ‘int’
abc.h:222: error: expected ‘)’ before ‘int’
Posted 26-Mar-12 4:49am
Edited 27-Mar-12 5:37am
v5
Comments
Pablo Aliskevicius at 26-Mar-12 11:12am
   
Which line is 222?
Are you using GCC?
What do you mean by ? Shouldn't the default be a class name?
enhzflep at 26-Mar-12 11:35am
   
Using gcc the messages are different and somewhat more helpful:

main.cpp|7|error: expected type-specifier before string constant
main.cpp|7|error: expected '>' before string constant
(line 7 is same as line 7 in above code snippet)

To fix, just remove the ="" from the 2nd class definition
Stefan_Lang at 27-Mar-12 11:19am
   
I modified the question to again show the source code originally posted. Whoever modified that code to something different just served to confuse everyone else...

P.S.: I also voted to make up for that unjustified 1-vote. No idea why this was voted down, it is a totally legitimate question.
Stefan_Lang at 27-Mar-12 11:39am
   
P.P.S.: modified it back to show
template <class dest_type="">

I was misled by the fact that viewing the original version did no show the last part - only viewing the differences from the first to the second revision let me see that the template argument list was actually there from the start ...
Stefan_Lang at 27-Mar-12 11:51am
   
Sorry for the confusion, but it seems that your various responses were not printed correctly as the < and > symbols required to show the template argument was incorrectly rendered as a (unknown, and therefore ignored) HTML tag by this site.

The response in solution 1 is correct: you cannnot use an empty string to initialize a template argument of type class. I believe I have seen something like that elsewhere (maybe Java?), but in C++ this syntax is not possible. There is no 'empty' class.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This part template<class dest_type="">, looks weird to me, are you trying to set the default type dest_type to an empty string?

Try removing that so that you're left with template<class dest_type>.

Hope this helps,
Fredrik
  Permalink  
Comments
Charmy from bangalore at 26-Mar-12 11:55am
   
class A {
public:
virtual ~A() {};
virtual int emit(int t, void* a)=0;
};

template //this is actual
class B : public A {
public:
typedef int (dest_type::*classpfn)(int, void*);
B(dest_type* pclass, classpfn pfn) : p(pclass), fn(pfn) {};
virtual int emit(int t, void* a) { return (p->*fn)(t, a); }
private:
dest_type *p;
classpfn fn;
};

Output:
=======
abc.h:222: error: expected unqualified-id before ‘int’
abc.h:222: error: expected ‘)’ before ‘int’
Iam getting error in these 2 lines
==================================
virtual int emit(int t, void* a)=0;
virtual int emit(int t, void* a) { return (p->*fn)(t, a); }
Charmy from bangalore at 26-Mar-12 11:55am
   
Iam using g++ compiler
nv3 at 26-Mar-12 14:08pm
   
I replaced your line

template // this is actual

with

template< class dest_type="">

Then it compiles without error on VC++. Neither your original version (with dest_type="") nor the version in the comment of an hour ago do compile though. Please try that line and if you still get an error, specify precisely which line the compiler dislikes.
Charmy from bangalore at 27-Mar-12 0:25am
   
class A {
public:
virtual ~A() {};
virtual int emit(int t, void* a)=0;
};
template
class B : public A {
public:
typedef int (dest_type::*classpfn)(int, void*);
B(dest_type* pclass, classpfn pfn) : p(pclass), fn(pfn) {};
virtual int emit(int t, void* a) {
return (p->*fn)(t, a);
}

Output:
=======
abc.h:222: error: expected unqualified-id before ‘int’
abc.h:222: error: expected ‘)’ before ‘int’
Iam getting error in these 2 lines in the function arguments
============================================================
virtual int emit(int t, void* a)=0;
virtual int emit(int t, void* a) { return (p->*fn)(t, a); }
Charmy from bangalore at 27-Mar-12 0:29am
   
Iam getting error in these 2 lines in the function arguments ============================================================
virtual int emit(int t, void* a)=0;
virtual int emit(int t, void* a) { return (p->*fn)(t, a); }
Stefan_Lang at 27-Mar-12 11:16am
   
The line

template

is likely the problem. The next character after this must be a < , followed by the template argument list, and then a >

Try this:

template <class dest_type>
nv3 at 27-Mar-12 5:55am
   
The compiler output is saying in both cases line 222, but the two lines you are showing in your last message are 8 lines apart. That is not consistent.

Also, I think we are loosing some characters in your program code that you did not escape properly. Please go to your original question and use the improve question button; then paste your C++ code in between <pre> tags. That makes sure that we see all characters, even the braces.
Stefan_Lang at 27-Mar-12 11:20am
   
The line you rightfully object about was modified by someone else. I changed it back to the original.
Stefan_Lang at 27-Mar-12 11:41am
   
P.S.: I apparently got it wrong. seems like inproper use (or interpretation) of HTML code got the better of me ...

Anyway, you're right, my 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 218
1 Sergey Alexandrovich Kryukov 170
2 Inimicos 60
3 ProgramFOX 50
4 Richard MacCutchan 45
0 OriginalGriff 9,037
1 Sergey Alexandrovich Kryukov 8,417
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,626


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 27 Mar 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100