May 9, 2010 at 12:09am UTC
You missed a ';' at line 13
May 9, 2010 at 12:10am UTC
Woops, but I still get the same error for line 42.
May 9, 2010 at 12:26am UTC
Indeed... this is weird... I've been looking at it for about 10 minutes and haven't figured it out yet...
May 9, 2010 at 12:28am UTC
Have you tried compiling it yourself? Maybe it's just my system.
May 9, 2010 at 12:33am UTC
Yes, I've tried... It doesn't compile... Look, you can do it like this until we find what's wrong, this one compiles:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <iostream>
#include <string>
#include <list>
#include <deque>
#include <vector>
using namespace std;
template <typename stlObject>
void PrintSTLObject(stlObject objectToPrint);
template <typename elementType>
void PrintSTLObject(list<elementType> objectToPrint);
int main()
{
deque<int > listIntegers;
listIntegers.push_front(10);
listIntegers.push_front(2001);
listIntegers.push_front(-1);
listIntegers.push_front(9999);
PrintSTLObject(listIntegers);
return 0;
}
template <typename stlObject>
void PrintSTLObject(stlObject objectToPrint)
{
for (unsigned int i = 0;
i < objectToPrint.size();
i++)
{
cout << "Element: " << i << ": " << objectToPrint.at(i) << endl;
}
}
template <typename elementType>
void PrintSTLObject(list<elementType> objectToPrint)
{
//list<elementType>::iterator iElementLocator; //right here
int i = 0;
/*for(iElementLocator = objectToPrint.begin();
iElementLocator != objectToPrint.end();
iElementLocator++)*/
for (i=0; objectToPrint.begin()+i!=objectToPrint.end(); i++)
{
cout << "Element " << i << ": " << *(objectToPrint.begin()+i) << endl;
++i;
}
}
EDIT: No, forget it... it won't work. It only compiles because it's a template and you don't use this function anywhere... If you instantiate a list in your main and try to call this function it won't compile...
Last edited on May 9, 2010 at 12:38am UTC
May 9, 2010 at 1:30am UTC
Well... It seems to me that this is the best you can do...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#include <iostream>
#include <deque>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
//template
template <typename stlObject>
void PrintSTLObject(stlObject objectToPrint);
//template specialization1
void PrintSTLObject(list<int > objectToPrint);
//template specialization2
void PrintSTLObject(list<double > objectToPrint);
int main()
{
deque<int > my_ideque;
vector<int > my_ivector;
list<int > my_ilist;
deque<double > my_ddeque;
vector<double > my_dvector;
list<double > my_dlist;
my_ideque.push_front(1);
my_ideque.push_front(2);
my_ideque.push_front(3);
my_ideque.push_front(4);
my_ddeque.push_front(1.1);
my_ddeque.push_front(2.2);
my_ddeque.push_front(3.3);
my_ddeque.push_front(4.4);
my_ivector.resize(4);
my_ilist.resize(4);
my_dvector.resize(4);
my_dlist.resize(4);
copy(my_ideque.begin(),my_ideque.end(),my_ivector.begin());
copy(my_ideque.begin(),my_ideque.end(),my_ilist.begin());
copy(my_ddeque.begin(),my_ddeque.end(),my_dvector.begin());
copy(my_ddeque.begin(),my_ddeque.end(),my_dlist.begin());
PrintSTLObject(my_ideque);
PrintSTLObject(my_ivector);
PrintSTLObject(my_ilist);
PrintSTLObject(my_ddeque);
PrintSTLObject(my_dvector);
PrintSTLObject(my_dlist);
return 0;
}
template <typename stlObject>
void PrintSTLObject(stlObject objectToPrint)
{
cout << "template call!" << endl;
for (unsigned int i = 0; i < objectToPrint.size(); i++)
{
cout << "Element: " << i << ": " << objectToPrint.at(i) << endl;
}
cout << endl;
}
void PrintSTLObject(list<int > objectToPrint)
{
cout << "template specialization1 call!" << endl;
list<int >::iterator iElementLocator;
int i = 0;
for (iElementLocator = objectToPrint.begin();
iElementLocator != objectToPrint.end();
iElementLocator++)
{
cout << "Element " << i << ": " << * iElementLocator << endl;
++i;
}
cout << endl;
}
void PrintSTLObject(list<double > objectToPrint)
{
cout << "template specialization2 call!" << endl;
list<double >::iterator iElementLocator;
int i = 0;
for (iElementLocator = objectToPrint.begin();
iElementLocator != objectToPrint.end();
iElementLocator++)
{
cout << "Element " << i << ": " << * iElementLocator << endl;
++i;
}
cout << endl;
}
Last edited on May 9, 2010 at 1:35am UTC
May 9, 2010 at 1:36am UTC
Looks like that will do. Thanks for all your effort.
May 9, 2010 at 1:38am UTC
No problem! :P Anytime! :D
May 9, 2010 at 5:06am UTC
*wince* full answers FTL.
I wonder why you were using typename. class would have been just fine, no?
-Albatross
May 9, 2010 at 6:10am UTC
In this case, typename and class are equivalent. It's purely the coders decision on which they like more.
May 11, 2010 at 6:22am UTC
I have an error problem as well, please help?
1>c:\documents and settings\student\my documents\visual studio 2008\projects\04shoemaker\04shoemaker\04shoemaker.cpp(9) : error C2447: '{' : missing function header (old-style formal list?)
code:
//programmer: Kelly Shoemaker
//program: 04Shoemaker.cpp
//purpose: Count Positive and Negative Values
#include <iostream>
using namespace std;
void main ();
{
cout << "Program Written By Kelly Shoemaker" << endl;
int i, v, p, n;
for (i = 1; i < = 10; i = i + 1);
{
cout << "Enter Integer Value: ";
cin >> v;
(v > = 0)?;
v = p p : n
cout << "Number of Positive Values: " << p << endl;
cout << "Number of Negative Values: " << n << endl;
}
system ("pause");
}
i am an extreme beginner
May 11, 2010 at 6:38am UTC
here is how to start your own topic.
Select the forum you want to post in.
Scroll down the page, and you will se a NEW TOPIC button.
(personally I think they should have that button at the top of the forum page).