473,385 Members | 1,673 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,385 software developers and data experts.

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

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
1 7214
* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Henrik S. Hansen | last post by:
I'm new to C++, and cannot figure out why this won't compile: std::map<std::string, int> tst; tst = 1; int main() { /*...*/ } It gives me: error: ISO C++ forbids declaration of `tst' with...
5
by: j0mbolar | last post by:
operator = (const char *string) { if(m_string) { free(m_string); m_string = 0; } if(string) { m_string = strdup(string); } }
4
by: Juhan Voolaid | last post by:
Hi I need help here. When i compile my program, i get this error: $ make g++ -c -Wall inf2_functions.cpp -o inf2_functions.o inf2_classes.h:6: error: ISO C++ forbids declaration of ‘vector’...
3
by: gamehack | last post by:
Hi all, Here's the error which I'm getting when trying to compile some code: boxmanager.h:16: error: ISO C++ forbids declaration of 'vector' with no type boxmanager.h:16: error: expected...
7
by: Florian Haag | last post by:
Hello, I'm trying to compile a programme which compiles fine under Linux; I'm trying it with MinGW G++ 3.4.2: Component.h: #ifndef COMPONENT_H_ #define COMPONENT_H_
6
by: mkborregaard | last post by:
Hi, I am getting an error message from MinGW that I just cannot figure what causes. The error message is: "Line 16: ISO C++ forbids declaration of 'AreaMap' with no type" My code is: #ifndef...
8
by: aneuryzma | last post by:
Hello, I'm merging an OpenCV app with an Ogre3d app. I'm on a mac, I'm using xCode. When I add #include "openCVApp.h" I got the following error:
6
by: samsneelam | last post by:
Hi.. This is samuel, while doing a program, i encountered this problem.. Let me give you clarity regarding my prob.. I am having two files .. one is mpcplib.h it contains the follwing...
10
by: tvnaidu | last post by:
I am using Three pthread functions below, I got ISO error, then I declared int variable called val123, then I assigned, but still I am getting error, any idea?. also I included pthread.h. compiling...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.