•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,571 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,617 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2899 | Replies: 2
![]() |
•
•
Join Date: Sep 2007
Posts: 14
Reputation:
Rep Power: 2
Solved Threads: 0
hi i have used the template in my simulation program but it generate the error, my program line of code that generate the error is (these code are in queue.template file)
template <class QueueItem>
queue <QueueItem>::~queue() // line25
{
}
template <class QueueItem>
void queue<QueueItem>::push(const QueueItem& entry) //line 42
{
}
template <class QueueItem>
std::ostream& operator << (std::ostream& out_s, const queue<QueueItem>& q)
{
typename queue<QueueItem> ::Node *QueueCursor; // line 85
// code here
}
ERROR generated are
queue.template:25: error: expected constructor, destructor, or type conversion before '<
queue.template:25: error: expected `;' before '<' token
queue.template:42: error: expected init-declarator before '<' token
queue.template:42: error: expected `;' before '<' token
queue.template:84: error: ISO C++ forbids declaration of `queue' with no type
queue.template: In function `std::ostream& CSCI30l_queue::operator<<(std::ostream&, int)
queue.template:85: error: expected nested-name-specifier before "queue"
queue.template:85: error: expected `(' before '<' token
queue.template:85: error: expected primary-expression before '>' token
queue.template:85: error: `::Node' has not been declared
queue.template:85: error: `QueueCursor' undeclared (first use this function)
queue.template:85: error: (Each undeclared identifier is reported only once for each fun )
queue.template:86: error: `q' undeclared (first use this function)
I could not figure out the problem
here the queue is the class that have the constructor and class itself defined in the queue.h file and this queue.template file is the implementation of the member function defined in the queue.h file
template <class QueueItem>
queue <QueueItem>::~queue() // line25
{
}
template <class QueueItem>
void queue<QueueItem>::push(const QueueItem& entry) //line 42
{
}
template <class QueueItem>
std::ostream& operator << (std::ostream& out_s, const queue<QueueItem>& q)
{
typename queue<QueueItem> ::Node *QueueCursor; // line 85
// code here
}
ERROR generated are
queue.template:25: error: expected constructor, destructor, or type conversion before '<
queue.template:25: error: expected `;' before '<' token
queue.template:42: error: expected init-declarator before '<' token
queue.template:42: error: expected `;' before '<' token
queue.template:84: error: ISO C++ forbids declaration of `queue' with no type
queue.template: In function `std::ostream& CSCI30l_queue::operator<<(std::ostream&, int)
queue.template:85: error: expected nested-name-specifier before "queue"
queue.template:85: error: expected `(' before '<' token
queue.template:85: error: expected primary-expression before '>' token
queue.template:85: error: `::Node' has not been declared
queue.template:85: error: `QueueCursor' undeclared (first use this function)
queue.template:85: error: (Each undeclared identifier is reported only once for each fun )
queue.template:86: error: `q' undeclared (first use this function)
I could not figure out the problem
here the queue is the class that have the constructor and class itself defined in the queue.h file and this queue.template file is the implementation of the member function defined in the queue.h file
Last edited by hectic : Oct 28th, 2007 at 10:37 pm.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
Your syntax needs help. See the C++ FAQ Lite.
The difference between a regular class or function and a template class or function is just that you have
in front of it, and can use Foo as a generic type instead of a specific type like int, or string or somesuch.
So:
can be made to print any printable thing by turning it into a template function:
Now I can print arrays of strings, floats, etc...
Compare the difference between the two functions, read your textbook over again, and look at the C++ FAQ Lite page I gave you. Also, use the word "typename" instead of "class" in your template declarations.
Hope this helps.
The difference between a regular class or function and a template class or function is just that you have
template<typename Foo>in front of it, and can use Foo as a generic type instead of a specific type like int, or string or somesuch.
So:
C++ Syntax (Toggle Plain Text)
//print an array of ints void printa( int a[], int len ) { for (int i = 0; i < len; i++) std::cout << a[ i ] << std::endl; }
C++ Syntax (Toggle Plain Text)
//print an array of any printable thing template<typename PrintableThing> void printa( PrintableThing a[], int len ) { for (int i = 0; i < len; i++) std::cout << a[ i ] << std::endl; }
Compare the difference between the two functions, read your textbook over again, and look at the C++ FAQ Lite page I gave you. Also, use the word "typename" instead of "class" in your template declarations.
Hope this helps.
Last edited by Duoas : Oct 28th, 2007 at 11:43 pm.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- init-declarator before "float" (C++)
- Error: Expected ')' (HTML and CSS)
- STL priority_queue with custom type...how do I do iit (C++)
- Declarator or Identifier Error (C)
- A problem with templates (C++)
Other Threads in the C++ Forum
- Previous Thread: Inheritance
- Next Thread: Depth first search of a labyrinth



Linear Mode