By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,386 Members | 1,751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,386 IT Pros & Developers. It's quick & easy.

ISO C++ forbids declaration of 'xxx' with no type

P: n/a
hello

i'm trying to implement some functionality whereby an algorithm in a
base template class relies on a function pointer supplied by a derived
template class. the types are only specified by the client (caller) of
the derived class.

i got it working under visual studio 2003/2005 but gcc 4.1.0
compilation fails. here's the smallest subset of the code that shows
the error:

template <class T>
class Foo {
protected:
typedef bool (*FuncPtr)(const T&);
virtual FuncPtr *getFuncPtr() = 0;

private:
// algorithm which invokes getFuncPtr()
// and does stuff with the pointer returned
// ...
};

// one example of a family of functions
// suitable for use in the algorithm:
template <class T>
bool func1(const T &x) { return x 1; }

// one example of a family of derived classes
// which implement getFuncPtr() plus other behavior:
template <class T>
class Bar : public Foo<T{
FuncPtr *getFuncPtr() {
static FuncPtr f = func1<T>;
return &f;
}
// other stuff...
};

int main() {
Bar<intb;
return 0;
}

here's the error returned by gcc:

[erik@laptop2 cpp_test]$ make
g++ -o test1 test1.cpp
test1.cpp:23: error: ISO C++ forbids declaration of
'FuncPtr' with no type
test1.cpp:23: error: expected ';' before '*' token
test1.cpp:28: error: expected `;' before '}' token
test1.cpp: In function 'int main()':
test1.cpp:31: error: cannot declare variable 'b' to
be of abstract type
'Bar<int>'
test1.cpp:22: note: because the following virtual
functions are pure within
'Bar<int>':
test1.cpp:6: note: bool (** Foo<T>::getFuncPtr())
(const T&) [with T = int]
make: *** [test1] Error 1
[erik@laptop2 cpp_test]$

what's wrong with the code syntactically?

is there a better way to do what i'm trying to do?

thanks,
eric

Jan 14 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
* eric:
>
i got it working under visual studio 2003/2005 but gcc 4.1.0
compilation fails. here's the smallest subset of the code that shows
the error:

template <class T>
class Foo {
protected:
typedef bool (*FuncPtr)(const T&);
virtual FuncPtr *getFuncPtr() = 0;

private:
// algorithm which invokes getFuncPtr()
// and does stuff with the pointer returned
// ...
};

// one example of a family of functions
// suitable for use in the algorithm:
template <class T>
bool func1(const T &x) { return x 1; }

// one example of a family of derived classes
// which implement getFuncPtr() plus other behavior:
template <class T>
class Bar : public Foo<T{
FuncPtr *getFuncPtr() {
static FuncPtr f = func1<T>;
return &f;
}
// other stuff...
};

int main() {
Bar<intb;
return 0;
}

here's the error returned by gcc:

[erik@laptop2 cpp_test]$ make
g++ -o test1 test1.cpp
test1.cpp:23: error: ISO C++ forbids declaration of
'FuncPtr' with no type
test1.cpp:23: error: expected ';' before '*' token
test1.cpp:28: error: expected `;' before '}' token
test1.cpp: In function 'int main()':
test1.cpp:31: error: cannot declare variable 'b' to
be of abstract type
'Bar<int>'
test1.cpp:22: note: because the following virtual
functions are pure within
'Bar<int>':
test1.cpp:6: note: bool (** Foo<T>::getFuncPtr())
(const T&) [with T = int]
make: *** [test1] Error 1
[erik@laptop2 cpp_test]$

what's wrong with the code syntactically?
It's the new-fangled two-phase compilation of templates introduced in
the 1998 standard.

In the first phase, before the template is instantiated with some
concrete type T, the compiler has no idea what 'FuncPtr' refers to in
class Bar.

One way to fix this is to add the line

typedef typename Foo<T>::FuncPtr FuncPtr;

somewhere near the top of class Bar.

Yep, it's ugly.

C++ templates generally are... ;-)
is there a better way to do what i'm trying to do?
Probably. For example, it's unclear why you're using a /pointer to a
pointer/ to a function, and it's unclear why you're using a function
pointer at all instead of a virtual member function, and it's unclear
why you're using the recurring template pattern. Without knowing more
about what you're trying to achieve it's difficult to advice.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 14 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.