473,399 Members | 3,832 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,399 software developers and data experts.

templated vector build-up

Thanks for helping on this problem

suppose I have the following class
class MyContent {

vector<MyType1 v1;
vector<MyType2 v2;
}

And I have a templated function
template <class Typefoo(vector<Tpye>& a, int type_code, MyContent&
content) {

if(type_code) {
//insert elements in 'a' into content.v1
for(int i = 0; i < a.size(); i++)
content.v1.push_back(a[i]);
} else {
// insert elements in 'a' into content.v2
for(int i = 0; i < a.size(); i++)
content.v2.push_back(a[i]);
}

}

But the compiler complains unmatched type!! How can I solve it?

Mar 1 '07 #1
3 1497
On Mar 1, 1:18 pm, "newbie" <mitbb...@yahoo.comwrote:
Thanks for helping on this problem

suppose I have the following class
class MyContent {

vector<MyType1 v1;
vector<MyType2 v2;

}

And I have a templated function
template <class Typefoo(vector<Tpye>& a, int type_code, MyContent&
content) {

if(type_code) {
//insert elements in 'a' into content.v1
for(int i = 0; i < a.size(); i++)
content.v1.push_back(a[i]);
} else {
// insert elements in 'a' into content.v2
for(int i = 0; i < a.size(); i++)
content.v2.push_back(a[i]);
}

}

But the compiler complains unmatched type!! How can I solve it?
I should mention that: Type passed into this function will always be
either MyType1 or MyType2.
Thanks

Mar 1 '07 #2
newbie wrote:
Thanks for helping on this problem

suppose I have the following class
class MyContent {

vector<MyType1 v1;
vector<MyType2 v2;
}

And I have a templated function
template <class Typefoo(vector<Tpye>& a, int type_code, MyContent&
content) {

if(type_code) {
//insert elements in 'a' into content.v1
for(int i = 0; i < a.size(); i++)
content.v1.push_back(a[i]);
} else {
// insert elements in 'a' into content.v2
for(int i = 0; i < a.size(); i++)
content.v2.push_back(a[i]);
}

}

But the compiler complains unmatched type!! How can I solve it?
You should post the code that calls the template function. You should
post the exact error message. And you should post the line number that
the error message refers to.

It possible you should really post a complete program, that will get
your question answered in seconds flat.

john
Mar 1 '07 #3
On Mar 1, 4:18 pm, "newbie" <mitbb...@yahoo.comwrote:
Thanks for helping on this problem
But we haven't done anything yet. ;-)
suppose I have the following class
class MyContent {

vector<MyType1 v1;
vector<MyType2 v2;

}

And I have a templated function
template <class Typefoo(vector<Tpye>& a, int type_code, MyContent&
content) {

if(type_code) {
//insert elements in 'a' into content.v1
for(int i = 0; i < a.size(); i++)
content.v1.push_back(a[i]);
} else {
// insert elements in 'a' into content.v2
for(int i = 0; i < a.size(); i++)
content.v2.push_back(a[i]);
}

}

But the compiler complains unmatched type!! How can I solve it?
The problem is that when the function is instantiated, "a" is
(presumably) either of MyType1 or MyType2. The former cannot be put in
v2 and the latter cannot be put in v1, but clearly you are trying to
do just that. Remember, all of the code in the function must compile,
regardless of what path will actually be taken at run-time.

Try this:

class MyContent
{
vector<MyType1 v1;
vector<MyType2 v2;
public:
void Add( const MyType1& a ) { v1.push_back( a ); }
void Add( const MyType2& a ) { v2.push_back( a ); }
}

template <class Type>
void foo(
const vector<Type>& a,
MyContent& content )
{
//insert elements in 'a' into content.v1
for(int i = 0; i < a.size(); i++)
content.Add( a[i] );
}

Note that type_code is gone. Static and dynamic polymorphism are
intended to replace such "type switch" constructs.

Cheers! --M

Mar 1 '07 #4

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

Similar topics

1
by: Rich | last post by:
Hi, I have a query regarding VC6 and its handling of templated copy constructors. Here goes: Take a look at the following code sample... template<class _Ty, size_t t_uiSize = 10 > class...
5
by: Marijn | last post by:
I recently told g++ (3.2) something to the effect of: template <class T> struct test{ typedef int type; }; template <class T> void f(){ test<T>::type i;
4
by: Tomas Ukkonen | last post by:
Hi I'm currently trying to make my old code to compile with new gcc 3.4.0. It compiles correctly in 3.3.3 so I'm not sure if this is compiler bug or not. Code very close to a example below...
5
by: Pete C. | last post by:
I'm trying to make a templated function a friend like this: class cls { ... friend cls func<> (const cls& a, const cls& b); }; template< template<class> class Op> cls func (const cls& a,...
2
by: Thomas Matthews | last post by:
Hi, I would like to create a table (or vector) of pointers to templated functions. 1. How do I declare a typedef of a pointer to a templated function? For example, I have some functions...
3
by: William Payne | last post by:
Consider this (templated) class member function: template<typename Type> void CircularContainer<Type>::insert(const Type& s) { vector<Type>::iterator itr = find(m_elements.begin(),...
3
by: case2005 | last post by:
Can anyone help with the following, I don't know if it's possible, but I'm certain there must be a standard way of dealing with this. I have the following: template<typename FooBar, typename...
4
by: hweekuan | last post by:
Hi, I got an error with gnu C++ but not with intel C++ (icc). The distilled code is given below, a variable "T" is stored as a const T& in the base class and when the derived class try to access...
5
by: Gert Van den Eynde | last post by:
Hi all, It's probably trivial but I can't figure it out... I have a templated class template <typename T, typename uclass A I wish to fill a vector with pointers to objects of class A. I...
2
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include...
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: 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
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...
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
agi2029
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.