473,395 Members | 1,639 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.

novice template query

bb
Hi,
On compiling the following simple template backed by 'std::vector', get
the following compile time error:

----- Compilation error ----------

tc2.h: In member function 'void MyArray<T>::display()':
tc2.h:36: error: expected `;' before 'ivi'
tc2.h:37: error: 'ivi' was not declared in this scope
tc2.h: In member function 'void MyArray<T>::display() [with T =
std::basic_string<char, std::char_traits<char>, std::allocator<char>
]': tc2.h:49: instantiated from here
tc2.h:36: error: dependent-name 'std::vector<T,std::allocator<_CharT>::iterator' is parsed as a non-type, but instantiation yields a type tc2.h:36: note: say 'typename std::vector<T,std::allocator<_CharT>::iterator' if a type is meant


------ Code --------------------------

template<typename T>
class MyArray {
public:
MyArray();
~MyArray();
void append(const T& item);
void display();
private:
std::vector<T> *v;
};

template<typename T>
MyArray<T>::MyArray() {
v = new std::vector<T>();
}

template<typename T>
MyArray<T>::~MyArray() {
delete v;
}

template<typename T>
void MyArray<T>::append(const T& item) {
v->push_back(item);
}

template<typename T>
void MyArray<T>::display() {

std::vector<T>::iterator ivi; // *** I know the issue is HERE ***
for(ivi = v->begin(); ivi != v->end(); ++ivi)
std::cout << *ivi << std::endl;
}

int main(int argc, char** argv) {

MyArray<std::string> ma1;
ma1.append("Test1");
ma1.append("Test2");

ma1.display();
}

------- Code ends -----------------------------------

Any comments/help please?

Thanks.

Apr 12 '06 #1
11 1847
bb wrote:
Hi,
On compiling the following simple template backed by 'std::vector', get
the following compile time error:

----- Compilation error ----------

tc2.h: In member function 'void MyArray<T>::display()':
tc2.h:36: error: expected `;' before 'ivi'
tc2.h:37: error: 'ivi' was not declared in this scope
tc2.h: In member function 'void MyArray<T>::display() [with T =
std::basic_string<char, std::char_traits<char>, std::allocator<char>

What happens if you include the required headers, <string> and <vector>?

Everything else looks to be OK.

--
Ian Collins.
Apr 12 '06 #2
bb
Hi,
The problem here is the iterator definition that the compiler doesn't
like.

template<typename T>
void MyArray<T>::display() {
std::vector<T>::iterator ivi; // *** I know the issue is
HERE ***
for(ivi = v->begin(); ivi != v->end(); ++ivi)
std::cout << *ivi << std::endl;
}

If I change it to be specific e.g. std::string as follows, it compiles
and runs ok. However, my question is how to keep it generic?.

std::vector<std:string>::iterator ivi;

Cheers.

Apr 12 '06 #3
bb wrote:
Hi,
The problem here is the iterator definition that the compiler doesn't
like.

template<typename T>
void MyArray<T>::display() {
std::vector<T>::iterator ivi; // *** I know the issue is
HERE *** typename std::vector<T>::iterator ivi; for(ivi = v->begin(); ivi != v->end(); ++ivi)
std::cout << *ivi << std::endl;
}

If I change it to be specific e.g. std::string as follows, it compiles
and runs ok. However, my question is how to keep it generic?.

std::vector<std:string>::iterator ivi;

Cheers.


iterator is a dependent type. The compiler doesn't know that iterator
is a type. Use typename. as shown above.
Apr 12 '06 #4
bb <mu**********@gmail.com> wrote:
----- Compilation error ---------- [snip] tc2.h:36: error: dependent-name 'std::vector<T,std::allocator<_CharT>
::iterator' is parsed as a non-type, but instantiation yields a type tc2.h:36: note: say 'typename std::vector<T,std::allocator<_CharT>
::iterator' if a type is meant


------ Code --------------------------

[snip] std::vector<T>::iterator ivi; // *** I know the issue is HERE ***


Your error message tells you how to fix the error; however, VS .NET 2003
compiled it as-is (after #include'ing <iostream>, <vector>, and
<string>).

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Apr 12 '06 #5
bb
Hi,
You are simply spot on. Thanks, it works.

I have another query in the same context...

template<typename T>
void showMe(T& t) {
std::cout << t << std::endl;
}

template<typename T>
void MyArray<T>::display2() {
for_each(v->begin(),v->end(),showMe<T>);
}

The above works fine as long as showMe() is a non-member template
function.

However, if I change showMe() to be a member of the template as
follows, it doesn't compile. I suspect the problem is in the calling
syntax. Any comments please?

template<typename T>
void MyArray<T>::showMe(T& t) {
std::cout << t << std::endl;
}

Thanks again.

Apr 12 '06 #6
Tip: Start a new thread for a new question.

bb wrote:
However, if I change showMe() to be a member of the template as
follows, it doesn't compile.


That's because a member function has a 'this'. The for_each (as called) has
no capacity to pass the 'this' in.

There are helper templates that bind the 'this' to the function object
passed to algorithms like for_each().

But why do you need the function to be a member? If it won't use the 'this',
or any members of its object, inside the function, then don't bother to
pass it in.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 12 '06 #7
bb
> Tip: Start a new thread for a new question.

Apologies, will do from next time.
That's because a member function has a 'this'. The for_each (as called) has
no capacity to pass the 'this' in. There are helper templates that bind the 'this' to the function object
passed to algorithms like for_each().
Precisely, can u throw some more light on this, please?
But why do you need the function to be a member? If it won't use the 'this',
or any members of its object, inside the function, then don't bother to
pass it in.


True. This is, probably, an over simplified version and not a good
example, hmmm.

Apr 12 '06 #8
bb
Thanks, yes, i realized it a bit late.

Apr 12 '06 #9
bb wrote:
Thanks, yes, i realized it a bit late.


Realized what?

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Apr 12 '06 #10

"Marcus Kwok" <ri******@gehennom.invalid> wrote in message
news:e1**********@news-int2.gatech.edu...
bb <mu**********@gmail.com> wrote:
----- Compilation error ---------- [snip]
tc2.h:36: error: dependent-name 'std::vector<T,std::allocator<_CharT>
::iterator' is parsed as a non-type, but instantiation yields a type

tc2.h:36: note: say 'typename std::vector<T,std::allocator<_CharT>
::iterator' if a type is meant


------ Code --------------------------

[snip]
std::vector<T>::iterator ivi; // *** I know the issue is HERE

***
Your error message tells you how to fix the error; however, VS .NET 2003
compiled it as-is (after #include'ing <iostream>, <vector>, and
<string>).

--
Marcus Kwok
Replace 'invalid' with 'net' to reply


Hmm, vc6.0sp4 couldn't compile the code with header and 'typename' added,
#include <vector>
#include <iostream>

template<typename T>
class MyArray {
public:
MyArray();
~MyArray();
void append(const T& item);
void display();
private:
std::vector<T> *v;
};

template<typename T>
MyArray<T>::MyArray() {
v = new std::vector<T>();
}

template<typename T>
MyArray<T>::~MyArray() {
delete v;
}

template<typename T>
void MyArray<T>::append(const T& item) {
v->push_back(item);
}

template<typename T>
void MyArray<T>::display() {

typename std::vector<T>::iterator ivi; // *** I know the issue is HERE
***
for(ivi = v->begin(); ivi != v->end(); ++ivi)
std::cout << *ivi << std::endl;
}

int main(int argc, char** argv) {

MyArray<std::string> ma1;
ma1.append("Test1");
ma1.append("Test2");

ma1.display();
}
Compiling...
template1.cpp
E:\Win32\tests\template1.cpp(45) : warning C4508: 'main' : function should
return a value; 'void' return type assumed
E:\Win32\tests\template1.cpp(35) : error C2679: binary '<<' : no operator
defined which takes a right-hand operand of type 'class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >' (or there is no acceptable conversio
n)
e:\program files\microsoft visual studio\vc98\include\xmemory(59) :
while compiling class-template member function 'void __thiscall
MyArray<class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > >::display(vo
id)'
Error executing cl.exe.

template1.obj - 1 error(s), 1 warning(s)
Apr 18 '06 #11
Fei Liu wrote:
"Marcus Kwok" <ri******@gehennom.invalid> wrote in message
news:e1**********@news-int2.gatech.edu...
bb <mu**********@gmail.com> wrote:
----- Compilation error ----------

[snip]
tc2.h:36: error: dependent-name 'std::vector<T,std::allocator<_CharT>
::iterator' is parsed as a non-type, but instantiation yields a type
tc2.h:36: note: say 'typename std::vector<T,std::allocator<_CharT>
::iterator' if a type is meant
------ Code --------------------------

[snip]
std::vector<T>::iterator ivi; // *** I know the issue is HERE

***
Your error message tells you how to fix the error; however, VS .NET 2003
compiled it as-is (after #include'ing <iostream>, <vector>, and
<string>).

--
Marcus Kwok
Replace 'invalid' with 'net' to reply


Hmm, vc6.0sp4 couldn't compile the code with header and 'typename' added,

[code redacted]


VC6 is pre-standard and has horrible template support. Upgrade to a
modern compiler.
Apr 18 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Simon G Best | last post by:
Hello! I have a query regarding explicit specialisation of class templates which are themselves members of class templates. Here's what I want to do: template< class T > struct pink { ...
2
by: CoolPint | last post by:
Can anyone kindly explain why non-type template parameters are required by giving some examples where their uses are clearly favourable to other alternatives? I cannot think of any good use for...
7
by: CoolPint | last post by:
While I was testing my understanding of Functioin Template features by playing with simple function templates, I got into a problem which I cannot understand. I would be very grateful if someone...
12
by: Roman Töngi | last post by:
I just want to raise a number to a power. e.g.: 16^2 The pow()-function of cmath seems to be not enough. I've read about valarray. Is it really so difficult? Can you give me an example. ...
3
by: J Smith | last post by:
I'm making a website where each page has the same design, obviously its a bad idea to put the same code/html in each page so what is the best way to do this? What I'm doing at the moment is...
5
by: catch | last post by:
Hi, Suppose I have a table something like: name (VARCHAR(64)) age (INT) quotation (TEXT) ================================================= Donald Trump 50 I am rich Bugs...
1
by: gretchen.ogrady | last post by:
I admit - I'm a simple user but looking to improve skills. Instructions aren't helping and have searched this group but am getting bogged down by some of the programming-speak. I have a query...
9
by: Kelii | last post by:
I've been trying to get this piece to work for a few hours, but have given up. I hope someone out there can help, I think the issue is relatively straightforward, but being a novice, I'm stumped....
7
by: Roarke | last post by:
Forgive me if this is too long/too simple - I am new to this! I have an htm page with a "post" form. This has 2 droplists, the variables of which are posted to a php page. This page queries a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.