Template trouble

  • Thread starter idar.douglas.hillgaar
  • Start date
I

idar.douglas.hillgaar

Hi - the following code compiled easily in VS.Net however using g++'s
there are several errors relating to template (I'm using a list()) -
using the latest ver. of g++
Code:
list <int> my_list2;
int counter;
const int size = 8;
int *seq = new int[size];

/* test values */
my_list2.push_back(1);
my_list2.push_back(2);
my_list2.push_back(3);
my_list2.push_back(1);
my_list2.push_back(3);
my_list2.push_back(2);
my_list2.push_back(2);
my_list2.push_back(3);

list <int>::iterator i;

if (my_list2.empty())
cout << "List is empty " << endl;
else
{

/* Adds integers to an array I need for 3rd lib. */
for( i = my_list2.begin(), count = 0; i != my_list2.end() && count <
size; i++, count ++)
{
seq[count] = *i;
}
}

/* printing out the values */
for (int i = 0; i < size; i++)
{
cout << " test: " << seq << endl;
}

Errors in g++:
readSequence.c: In function 'int main()':
readSequence.c:561: error: expected primary-expression before
'template'
readSequence.c:561: error: expected `;' before 'template'
readSequence.c:567: error: expected initializer before '*' token
readSequence.c:570: error: 'my_list2' was not declared in this
scope
readSequence.c:584: error: overloaded function with no contextual type
information
readSequence.c:584: error: parse error in template argument list
readSequence.c:584: error: expected `;' before ')' token
readSequence.c:586: error: 'seq' was not declared in this scope
readSequence.c:592: error: 'seq' was not declared in this scope
make: *** [readSequence.o] Error 1
 
A

Alf P. Steinbach

* (e-mail address removed):
Hi - the following code

Is not your actual code. Post a complete, minimal, actual example, and
indicate where in that code the compiler reports an error.
 
A

Amadeus W. M.

Errors in g++:
readSequence.c: In function 'int main()':
readSequence.c:561: error: expected primary-expression before
'template'

So go to line 561 and see what the problem might be.
Very likely fixing this might fix (some of) the subsequent
errors.
 
D

dougie

The problem is at the for loop in the test code and my own code:

for( i = my_list2.begin(), count = 0; i != my_list2.end() && count <
size; i++, count ++)
where I get the following error:
" overloaded function with no contextual type information"
 
D

dougie

solved!:

list <int>::iterator i; should be: list<int>::iterator i;
the problem was the whitespace
 
A

Alf P. Steinbach

* dougie:
* dougie:

solved!:

list <int>::iterator i; should be: list<int>::iterator i;
the problem was the whitespace

Uh, good, problem solved, but bad, very bad!, that whitespace shouldn't
really matter. Perhaps what's lacking is to write 'std::list'? And
what about the 'count' (usage) versus 'counter' (declaration)?

This compiles cleanly using MSVC 7.1 and g++ 3.4.4, and should compile
cleanly on any conforming C++ implementation:

#include <list>

int main()
{
using namespace std;
list <int>::iterator i;
}

Btw., please don't top-post in this group.

See the FAQ item "How can I find out about general netiquette so I don't
embarrass myself?", currently at <url:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4>.
 
T

Thomas J. Gritzan

Hi - the following code compiled easily in VS.Net however using g++'s
there are several errors relating to template (I'm using a list()) -
using the latest ver. of g++
Code:
list <int> my_list2;
int counter;
const int size = 8;
int *seq = new int[size];
[...]

When using std::list for the list, why don't you use std::vector for the
array? You can pass the address to the first element to the 3rd party
library:

std::vector seq;

library_function(&seq[0], seq.size());
list <int>::iterator i;

Please don't call an iterator "i". IMHO, i is the dogmatic name for an
integer variable.
I would call an iterator it or itor, or a better name depending on what
the iterator is used for.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,933
Members
47,472
Latest member
blackwatermelon

Latest Threads

Top