I have the following code that works on Visual C++ 2005 and on GCC.
bool VectorContains( const std::vector<int >& vect, int value )
{
std::vector<int >::const_iterat or iter;
for( iter = vect.begin(); iter != vect.end(); iter++ )
{
if( *iter == value )
{
return true;
}
}
return false;
}
If i try and template this, I get the following, but it only compiles on
Visual Studio, not on GCC. The error occurs when the calling code is
using an 'int' as 'T'; I have not tried others.
Anyone have any ideas? Is this a c++ syntax error on my part or should
this work? Is there a better std way to do the same thing?
template < typename T >
inline bool VectorContains( const std::vector<T>& vect, T value )
{
std::vector<T>: :const_iterator iter;
for( iter = vect.begin(); iter != vect.end(); iter++ )
{
if( *iter == value )
{
return true;
}
}
return false;
}
error:
In file included from mmlib/manipulators/conformerbuilde rbase.cpp:4:
../mmlib/tools/vector.h: In function `bool VectorContains( const
std::vector<T, std::allocator< _CharT&, T)':
../mmlib/tools/vector.h:72: error: expected `;' before "iter"
../mmlib/tools/vector.h:74: error: `iter' undeclared (first use this
function)
../mmlib/tools/vector.h:74: error: (Each undeclared identifier is
reported only once for each function it appears in
..)
make: *** [mmlib/manipulators/conformerbuilde rbase.o] Error 1
Cheers,
Jon Rea 4 1961
Jon Rea napsal:
I have the following code that works on Visual C++ 2005 and on GCC.
bool VectorContains( const std::vector<int >& vect, int value )
{
std::vector<int >::const_iterat or iter;
for( iter = vect.begin(); iter != vect.end(); iter++ )
{
if( *iter == value )
{
return true;
}
}
return false;
}
If i try and template this, I get the following, but it only compiles on
Visual Studio, not on GCC. The error occurs when the calling code is
using an 'int' as 'T'; I have not tried others.
Anyone have any ideas? Is this a c++ syntax error on my part or should
this work? Is there a better std way to do the same thing?
template < typename T >
inline bool VectorContains( const std::vector<T>& vect, T value )
{
std::vector<T>: :const_iterator iter;
Here you need
typename std::vector<T>: :const_iterator iter;
Visual studio accepts it without keyword typename, but it is not
correct.
for( iter = vect.begin(); iter != vect.end(); iter++ )
{
if( *iter == value )
{
return true;
}
}
return false;
}
Jon Rea wrote:
I have the following code that works on Visual C++ 2005 and on GCC.
bool VectorContains( const std::vector<int >& vect, int value )
{
std::vector<int >::const_iterat or iter;
for( iter = vect.begin(); iter != vect.end(); iter++ )
{
if( *iter == value )
{
return true;
}
}
return false;
}
If i try and template this, I get the following, but it only compiles on
Visual Studio, not on GCC. The error occurs when the calling code is
using an 'int' as 'T'; I have not tried others.
Anyone have any ideas? Is this a c++ syntax error on my part or should
this work? Is there a better std way to do the same thing?
template < typename T >
inline bool VectorContains( const std::vector<T>& vect, T value )
{
std::vector<T>: :const_iterator iter;
for( iter = vect.begin(); iter != vect.end(); iter++ )
{
if( *iter == value )
{
return true;
}
}
return false;
}
error:
In file included from mmlib/manipulators/conformerbuilde rbase.cpp:4:
./mmlib/tools/vector.h: In function `bool VectorContains( const
std::vector<T, std::allocator< _CharT&, T)':
./mmlib/tools/vector.h:72: error: expected `;' before "iter"
./mmlib/tools/vector.h:74: error: `iter' undeclared (first use this
function)
./mmlib/tools/vector.h:74: error: (Each undeclared identifier is
reported only once for each function it appears in
.)
make: *** [mmlib/manipulators/conformerbuilde rbase.o] Error 1
Cheers,
Jon Rea
More info on this - if i change the code to this:
template < typename T >
inline
bool VectorContains( const std::vector<T>& vect, T value )
{
typedef std::vector<T>: :const_iterator bob;
for(
bob iter = vect.begin();
iter != vect.end();
iter++ )
{
if( *iter == value )
{
return true;
}
}
return false;
}
I get the error:
In file included from mmlib/manipulators/conformerbuilde rbase.cpp:4:
../mmlib/tools/vector.h: In function `bool VectorContains( const
std::vector<T, std::allocator< _CharT&, T)':
../mmlib/tools/vector.h:72: error: expected init-declarator before "bob"
../mmlib/tools/vector.h:72: error: expected `,' or `;' before "bob"
../mmlib/tools/vector.h:74: error: `bob' undeclared (first use this function)
../mmlib/tools/vector.h:74: error: (Each undeclared identifier is
reported only once for each function it appears in
..)
../mmlib/tools/vector.h:74: error: expected `;' before "iter"
../mmlib/tools/vector.h:75: error: `iter' undeclared (first use this
function)
make: *** [mmlib/manipulators/conformerbuilde rbase.o] Error 1
Cheers,
Jon
Ondra Holub wrote:
Jon Rea napsal:
>I have the following code that works on Visual C++ 2005 and on GCC.
bool VectorContains( const std::vector<int >& vect, int value ) { std::vector<int >::const_iterat or iter; for( iter = vect.begin(); iter != vect.end(); iter++ ) { if( *iter == value ) { return true; } } return false; }
If i try and template this, I get the following, but it only compiles on Visual Studio, not on GCC. The error occurs when the calling code is using an 'int' as 'T'; I have not tried others.
Anyone have any ideas? Is this a c++ syntax error on my part or should this work? Is there a better std way to do the same thing?
template < typename T > inline bool VectorContains( const std::vector<T>& vect, T value ) { std::vector<T>: :const_iterator iter;
Here you need
typename std::vector<T>: :const_iterator iter;
Visual studio accepts it without keyword typename, but it is not
correct.
> for( iter = vect.begin(); iter != vect.end(); iter++ ) { if( *iter == value ) { return true; } } return false; }
Thank you very much, works great!
Visual studio does annoy me with its lack of standards compliance... I
make things work in it and they don't work elsewhere ... Grrr ;-)
Cheers,
Jon
On Jan 29, 11:40 am, Jon Rea <jon....@bris.a c.ukwrote:
I have the following code that works on Visual C++ 2005 and on GCC.
[snip]
template < typename T >
inline bool VectorContains( const std::vector<T>& vect, T value )
{
std::vector<T>: :const_iterator iter;
for( iter = vect.begin(); iter != vect.end(); iter++ )
{
if( *iter == value )
{
return true;
}
}
return false;
}
[snip]
Others have pointed out the missing typename. I just want to suggest
you to look at std::find located in <algorithmtha t does almost what
you want to (returning an iterator - possibly to end). You could use
that function instead - either directly or in the body of your
VectorContains. There is no reason to reinvent the wheel, the built in
function is possibly faster and the code becomes easier to
understand.
/Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mat DeLong |
last post by:
I have never been stuck on programming something before to the point I give
up... this is a first. I am programming what should be something very easy
in C++... using Templates. Here is the code, and the error I get:
--------------
namespace LIST {
template <typename T> struct Link;
template <typename T> class List;
|
by: Chris |
last post by:
I am having a very strange problem involving virtual
functions in template classes. First of all, here is an
extremely simplified structure of the two classes I am
having problems with.
template<class Type> class base
{
public:
base& operator/=(const base&);
Type *image;
|
by: ranges22 |
last post by:
******************************************************************
I am compiling a librarry which has a .h file containing th
following:
******************************************************************
template<typename T> void from_string(const char Str, T &Obj);
template<> void from_string(const char Str, long &); //
template<> void from_string(const char Str, unsigned lon
&); //
|
by: Rudy Ray Moore |
last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge
ammounts of "template assistance" error messaging referencing template code
(MTL) that has nothing to do with the error.
This makes it difficult to spot errors. For example, F4 to "jump to next
error" just jumps ot the next "template assistance" message. And since
there are hundreds of them, this is quite obnoxious.
Can I disable these things? Why is MSVC...
|
by: Greg Buchholz |
last post by:
/*
I've been experimenting with some generic/polytypic programs, and
I've stumbled on to a problem that I can't quite figure out. In the
program below, I'm trying to define a generic version of "transform"
which works not only on lists, but lists of list, lists of lists of
lists, etc. I'm calling it "fmap" and it passes around actual lists
instead of iterators (for now). In order to get it to work, I thought
I'd have one templated...
| |
by: pagekb |
last post by:
Hello,
I'm having some difficulty compiling template classes as containers for
other template objects. Specifically, I have a hierarchy of template
classes that contain each other. Template class B has an instance of
template class A, which has some base type T (usually int or double).
However, the base type T is important to calculations in B, so I would
like to obtain access to the type for further variable declaration
within B. ...
|
by: Hamilton Woods |
last post by:
Diehards,
I developed a template matrix class back around 1992 using Borland C++ 4.5
(ancestor of C++ Builder) and haven't touched it until a few days ago. I
pulled it from the freezer and thawed it out. I built a console app using
Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the
header file had to be commented out.
I built a console app using Borland C++ Builder 5. The linker complained of
references to...
|
by: Jess |
last post by:
Hi,
I have a template function that triggered some compiler error. The
abridged version of the class and function is:
#include<memory>
using namespace std;
template <class T>
|
by: Barry |
last post by:
The following code compiles with VC8
but fails to compiles with Comeau online,
I locate the standard here:
An explicit specialization of any of the following:
|
by: QiongZ |
last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template classes. One is List1, the other one is ListNode. The codes are below:
// test-linked-list.cpp :...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |