Connecting Tech Pros Worldwide Forums | Help | Site Map

Template trouble

idar.douglas.hillgaar@gmail.com
Guest
 
Posts: n/a
#1: Aug 5 '06
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 <intmy_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[i] << 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

Alf P. Steinbach
Guest
 
Posts: n/a
#2: Aug 5 '06

re: Template trouble


* idar.douglas.hillgaar@gmail.com:
Quote:
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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Amadeus W. M.
Guest
 
Posts: n/a
#3: Aug 5 '06

re: Template trouble


Quote:
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.

dougie
Guest
 
Posts: n/a
#4: Aug 5 '06

re: Template trouble


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"


Alf P. Steinbach wrote:
Quote:
* idar.douglas.hillgaar@gmail.com:
Quote:
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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
dougie
Guest
 
Posts: n/a
#5: Aug 5 '06

re: Template trouble


solved!:

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


dougie wrote:
Quote:
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"
>
>
Alf P. Steinbach wrote:
>
Quote:
* idar.douglas.hillgaar@gmail.com:
Quote:
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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Alf P. Steinbach
Guest
 
Posts: n/a
#6: Aug 5 '06

re: Template trouble


* dougie:
Quote:
* dougie:
Quote:
>* Alf P. Steinbach wrote:
Quote:
>>* idar.douglas.hillgaar@gmail.com:
>>>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.
>>
>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"
>
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>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Thomas J. Gritzan
Guest
 
Posts: n/a
#7: Aug 6 '06

re: Template trouble


idar.douglas.hillgaar@gmail.com schrieb:
Quote:
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 <intmy_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());
Quote:
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.

--
Thomas
Closed Thread