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

templates, scope resolution and g++

I have some code (a doubly-linked list implemented as a template) which is generating some bizarre errors.

On Digital Mars C++ (v. 8.42) the code compiles but I have to add what seems to be a superfluous template <typename T> right before the declaration of a friend function to overload <<.

Expand|Select|Wrap|Line Numbers
  1.     template <typename T>
  2.         friend std::ostream & operator<<(std::ostream & os, 
  3.         const DLink2<T> * dlink);
  4.  
In gcc (via mingw, v.3.4.2), the compiler "suggests" putting a <> after the name of the function to suppress a warning (which seems to cause more trouble). In addition to that I get the following error message.

Expand|Select|Wrap|Line Numbers
  1. template <typename T>
  2. DLink2<T>::Node * DLink2<T>::Link(Node * lastlink,T value,bool before) 
error: expected constructor, destructor, or type conversion before '*' token


Node is a private class within the DLink2 class.

Expand|Select|Wrap|Line Numbers
  1. class DLink2
  2. {
  3.  
  4.     private:
  5.     class Node
  6.     {    
  7.         private:
  8.             Node * _pNext;
  9.             Node * _pPrev;
  10.                 T    data;
  11.                   ........etc.

My questions are: I know that gcc and derivatives are more ISO compliant, but what is it about what I was doing that made a difference? And, is there a way to further resolve the scope for the function definition to recognize the Node type as a return type?
Dec 23 '08 #1
11 3150
weaknessforcats
9,208 Expert Mod 8TB
You might post more of your code. I can't tell if the class DLink2 is a template or not. If it's not the T in private Node class can't be resolved.
Dec 23 '08 #2
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. template<typename T>
  3. class DLink2
  4. {
  5.  
  6.     private:
  7.     class Node
  8.     {    
  9.         private:
  10.             Node * _pNext;
  11.             Node * _pPrev;
  12.                 T    data;    
  13.  
  14.         public:
  15.             Node(T input) {this->data = input;}
  16.             Node(){this->data = 0;}
  17.             Node * getNext() {return this->_pNext;}
  18.             Node * getPrev() {return this->_pPrev;}
  19.             void setNext(Node * nextNode) 
  20.                     {this->_pNext = nextNode;}
  21.             void setPrev(Node * prevNode) 
  22.                     {this->_pPrev = prevNode;}
  23.             T getData() {return this->data;}
  24.             void setData(T value){this->data = value;}
  25.     };
  26.  
  27.     protected:
  28.     Node * _pHead;
  29.     Node * _pTail;
  30.  
  31.  
  32.     public:
  33.         DLink2(int num_elements);
  34.         DLink2(int num_elements,T * element_array);
  35.         DLink2();
  36.         ~DLink2();
  37.  
  38.         Node * Link(Node * lastlink,bool before);    
  39.         Node * Link(Node * lastlink,T value,bool before);
  40.  
  41.         void InsertEnd(T value,bool before);
  42.  
  43.         template <typename T>
  44.         friend std::ostream & operator<<(std::ostream & os, 
  45.         const DLink2<T> * dlink);
  46. };
  47.  
  48.  
The definitions follow in the same file... there is a separate main.cpp where I instantiate two objects and output them via cout.

Thanks again.
Dec 23 '08 #3
boxfish
469 Expert 256MB
Try this:
Expand|Select|Wrap|Line Numbers
  1. friend std::ostream & operator<< <> (std::ostream & os, const DLink2<T> * dlink);
I don't know what the pair of angle brackets means, but hopefully they will make it compile.
Dec 23 '08 #4
DM seems to take either the way I specified above (with the template <typename T> immediately before the friend declaration) or the way you have proposed with the two angle brackets in the middle.

g++ doesn't balk about the DLink2<T>::Node in the return type anymore but balks when I declare DLink2<T>::Node in the ostream << friend function (along with DLink2<T>::_pHead and _pTail (which I've declared as protected). Not sure if I'm still missing something.

P.S. could someone explain the notation with the empty angle brackets after the function to me
Dec 23 '08 #5
I've added in the change that boxfish suggested, and I think that I'm still having some problems with the template business overall as compiling under g++ still gives me the following error:

LOCALS~1\Temp/ccGyaaaa.o(.text$_ZN6DLink2IiEC1EiPi[DLink2<int>::
DLink2(int, int*)]+0x1b0): In function `ZSt17__verify_groupingPKcjRKSs':
c:/mingw/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/locale_
facets.tcc:2510: undefined reference to `DLink2<int>::Link(DLink2<int>::Node*, i
nt, bool)'
LOCALS~1\Temp/ccGyaaaa.o(.text$_ZN6DLink2IiEC1Ei[DLink2<int>::DL
ink2(int)]+0x173):c:/mingw/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/
3.4.2/bits/locale_facets.tcc:2510: undefined reference to `DLink2<int>::Link(DLi
nk2<int>::Node*, bool)'
collect2: ld returned 1 exit status

Anyhow, I'm thinking there is still some underlying problem as it is reporting those errors in the main function (the template class is declared and defined in a separate header file).
Dec 24 '08 #6
weaknessforcats
9,208 Expert Mod 8TB
The code from the OP post #16 compiles and links using Visual Studio.NET 2008.
Dec 24 '08 #7
Not sure what OP#16 refers to, but I am also wondering why g++ is saying these functions are undefined once I get to main(). Could this be a specific problem with the compiler?

Thanks and happy holidays to all.
Dec 24 '08 #8
weaknessforcats
9,208 Expert Mod 8TB
OP #16 refers to the Original Poster (that's you) and Post #16 (in the upper right corner).
Dec 25 '08 #9
weaknessforcats
9,208 Expert Mod 8TB
Actually, I screwed that up. The site has change3d the posting format. I should have said OP #3. The 16 was the OP number of posts.

Sorry.
Dec 25 '08 #10
So, is this case something that DM is letting go that it shouldn't or an enforcement by gcc that shouldn't be in place?

Also, why the <> after the function name (operator<< in this case)?
Dec 29 '08 #11
weaknessforcats
9,208 Expert Mod 8TB
When you have a function template the compiler needs to know what type to substitute use when it makes its copy of the template. Usually this is done by looking at the function arguments. However, it can happen that none of the arguments are a parameterized type and this lack requires a means to tell the compiler what type to substitute. Therefore, you code MyFunct<int>() to pass this info to the compiler. You already do this for class variables so the syntax is consistent with that.
Dec 29 '08 #12

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

Similar topics

5
by: exits funnel | last post by:
Hello, I'm confused by the use of the Scope resolution operator on the indicated lines in the following code (which was copied from Thinking in C++ by Bruce Eckel). Removing them seems to have...
6
by: Mike | last post by:
Hey! I've started to use templates for storing arrays of doubles. The template itself will then be passed on to a linked-list. My problem is that Im not 100% familiar with how templates are...
0
by: Howard Gardner | last post by:
/* I don't have to provide scope resolution in sit 0 below: adl finds the function that I mean to use. I do have to provide scope resolution in sits 1 and 2. Can anyone teach me a trick to...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
6
by: Jack | last post by:
I want to write code that can be compiled as C and C++. In the Windows API, there are many macros that help with scope resolution. For example: #ifdef __cplusplus #define SNDMSG ::SendMessage...
5
by: Gianni Mariani | last post by:
I'm hoping someone can tell me why using member address of works and why using the dot operator does not in the code below. The code below uses the template function resolution mechanism to...
3
by: Szabolcs | last post by:
Consider the attached example. When I try to compile it (with g++ 4.1.2), I get the error message error: no matching function for call to 'fun(A<int>&)' However, if I do not use templates, it...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
68
by: DaveJ | last post by:
Recently I was working on a project where I came across an issue where the program cored and reported "free(): invalid pointer". I found a resolution for this, but don't fully understand why the...
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: 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.