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

Expected template-name before ‘<’ token.Template class inheritance and virtual method

5
Hi all,
I have C++ source code that compiles on windows-MS VS2005 without any problem. I am trying to compile it in Linux gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) and I ran into the following compiler error:

error: expected template-name before ‘<’ token
error: expected `{' before ‘<’ token
error: expected unqualified-id before ‘<’ token

(the error points to the line containing class myMarchingSurface3D:public CMarchingSurface3D<U> in the derived class)

I have looked in other threads and it seems inheritance with template classes gives some portability problems. I attach the code here:

Expand|Select|Wrap|Line Numbers
  1. template<class U> 
  2. class CMarchingSurface3D
  3. {
  4.    [...other methods]
  5. public:
  6.   inline virtual CVector3D<TV3DFLOAT> GetVoteDir(CPoint3D<INT16> &p, CTokenGroup3D<U>& tokengroup) { return CVector3D<TV3DFLOAT>(0,0,0); }
  7.  
  8. }
  9.  
  10. // implement the virtual functions here for
  11.     // marching surface.
  12. class myMarchingSurface3D:public CMarchingSurface3D<U>{
  13.     public:
  14.  
  15.       [...othe rmethods]
  16.         inline CVector3D<TV3DFLOAT> GetVoteDir(CPoint3D<INT16>& p, CTokenGroup3D<U>& tokengroup) {
  17.             //collect the the stick vote from neighbour
  18.             CTensorVote3D<TV3DFLOAT> vote=
  19.                 m_kernal->GetAllStickVotesTensor(p,tokengroup,*m_stickfield);
  20.             vote.Split();
  21.             // we use the sqrt(Lambda1)-sqrt(Lambda2) for voxel strength
  22.             m_lastSal=sqrt(vote.GetLambda1())-sqrt(vote.GetLambda2());
  23.             return vote.GetE1();
  24.         }
  25.     };

Thanks for your help in advance,
Fernando
Nov 4 '07 #1
10 29074
weaknessforcats
9,208 Expert Mod 8TB
class myMarchingSurface3D:public CMarchingSurface3D<U>{
public:

[...othe rmethods]
You need to declare this class as a template to support inheriting from CMarchingSurface3D<U>. Otherwise, the compiler thinks U is a real type:
Expand|Select|Wrap|Line Numbers
  1. template <class U>
  2. class myMarchingSurface3D:public CMarchingSurface3D<U>{
  3. public:
  4.  
  5. [...othe rmethods]
  6.  
Also, there are no inline virtual functions. Virtual functions have their addresses kept in a virtual function table (VTBL) to support polymorphism.

Inline functions are merely copied inline where they are called. They don't have addresses. Since C++ sees the inline keyword as a suggestion by you, it doesn't necessarily have to agree to it. In this case, the inline is ignored in favor of the virtual keyword.

Personally, I would remove the inline keyword from the templates.
Nov 4 '07 #2
famat
5
You need to declare this class as a template to support inheriting from CMarchingSurface3D<U>. Otherwise, the compiler thinks U is a real type:
Expand|Select|Wrap|Line Numbers
  1. template <class U>
  2. class myMarchingSurface3D:public CMarchingSurface3D<U>{
  3. public:
  4.  
  5. [...othe rmethods]
  6.  
Also, there are no inline virtual functions. Virtual functions have their addresses kept in a virtual function table (VTBL) to support polymorphism.

Inline functions are merely copied inline where they are called. They don't have addresses. Since C++ sees the inline keyword as a suggestion by you, it doesn't necessarily have to agree to it. In this case, the inline is ignored in favor of the virtual keyword.

Personally, I would remove the inline keyword from the templates.
Hi,
Thanks for the quick reply and the tips. I added template<class U> as suggested but I still get the same error from the gcc compiler. Any idea what can be the problem?

Thanks,
Fernando
Nov 4 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
No. I got it to compile using Visual Studio.NET 2005. I know very little about gcc. I assume you are compiling as g++ to get the C++ compiler?
Nov 5 '07 #4
famat
5
No. I got it to compile using Visual Studio.NET 2005. I know very little about gcc. I assume you are compiling as g++ to get the C++ compiler?
Hi,
Yes, I am using g++. The problem seems to be somehow with the standards. As I said in my first post I compile without any problem using MS-VS. However, GNU gcc wants things different. But after trying many different things I don't know how to put them different. Any suggestion?

Thanks,
Fernando
Nov 5 '07 #5
Laharl
849 Expert 512MB
I fixed a similar problem by templating with

Expand|Select|Wrap|Line Numbers
  1. template<typename U>
  2. class A {
  3.  
Using typename rather than class or classname seems to make g++ happier for some reason...
Nov 5 '07 #6
famat
5
I fixed a similar problem by templating with

Expand|Select|Wrap|Line Numbers
  1. template<typename U>
  2. class A {
  3.  
Using typename rather than class or classname seems to make g++ happier for some reason...
Hi,
Where do I have to change template<class U> by template<typename U>? Everywhere (in all the .cpp implementing the methods for teh template class?)? What is the difference between typename and class?

I tried to change class U by typename U only in the declaration and teh compiler still gives the same error.

Thanks,
Fernando
Nov 5 '07 #7
Laharl
849 Expert 512MB
Everywhere you have template<class U>, change it to template<typename U>.

I can't guarantee this will work, but I've had success with it, as the book I learned from used <classname U> and that gave me errors until I switched to typename.
Nov 6 '07 #8
famat
5
Everywhere you have template<class U>, change it to template<typename U>.

I can't guarantee this will work, but I've had success with it, as the book I learned from used <classname U> and that gave me errors until I switched to typename.
Hi,
I tried to change it everywhere and still gives the same error. However, the code compiles with gcc-3.3.5 (I just tried it) but not with gcc-4.1.2. Any idea what happens? This is very frustrating:(

Thanks,
Fernando
Nov 7 '07 #9
Laharl
849 Expert 512MB
Frankly, I can't tell you any more, though I have had some issues with templates on later versions of gcc.
Nov 7 '07 #10
weaknessforcats
9,208 Expert Mod 8TB
Everywhere you have template<class U>, change it to template<typename U>.

I can't guarantee this will work, but I've had success with it, as the book I learned from used <classname U> and that gave me errors until I switched to typename.
class U and typename U are equivalent. typename is generally used by template writers whio are not using classes.

There is no such thing as classname. That is a synrtax error.

On g++, has anyone tried the -pedantic switch? That disables non-ANSI extensions to C++.
Nov 7 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: shank | last post by:
I'm getting an Expected 'End' error on the following. What did I forget? <% p = rsSpecs("OrderNo") & chr(13) & chr(10) p = p & rsSpecs("Description") & chr(13) & chr(10) p = p & media &...
3
by: Matt | last post by:
When the ASP statement end with a _ character, then the next line cannot have comment ' character. Is that correct? Since I encountered the following error: Microsoft VBScript compilation...
13
by: Squid Seven | last post by:
This is just bizarre. for the following snippet of code: #include <string> using std::string; I get the error message:
6
by: David Cook | last post by:
The html file below gets intermittent errors 'error on page' ('number expected') when clicking on column-headings to sort. Yet, this same file works flawlessly in other browsers (i.e. Opera,...
3
by: undercups | last post by:
When I run page I immediatley get "Object expected" message and the tag <body onload="setfocus()" is highlighted. The error occurs in this snippet from within the script file function...
0
by: Sameers (theAngrycodeR) via .NET 247 | last post by:
I think I will get MAD very soon. Its very weired problem I amfacing here. Let me explain. I have two DBF files from which I want to export data in CSVformat. I created a desktop application and...
1
by: liam | last post by:
Hi, I have seen this problem is a couple of other places in the group but can't find an answer to it. Basically I have as aspx file that opens properly in Opera but not in IE. IE gives a...
4
by: Mark | last post by:
I have written a PocketPC application that connects to a web service, and when im connected via the cable or via wireless then it pulls back the data correctly. However when connected to GPRS, i...
1
by: JOJO123 | last post by:
I got here in search of an answer to this Javascrpt question. I upgraded jave on XP Ie 7, acrobat 5.1 and suddenly can't open any pdf files on web sites using IE. I see u guys all say, this is a...
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...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.