473,385 Members | 1,919 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,385 software developers and data experts.

list::insert(iterator position, size_type n, const & x); failed to compile

Hi,

I got the following compile errors when I used STL
list::insert ( iterator position, size_type n, const T& x );

==================
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <list>
  3.  
  4. main()
  5. {
  6.    list<int> myList;
  7.    list<int>::iterator it;
  8.  
  9.    myList.push_back(0);
  10.    it = myList.begin();
  11.  
  12.    myList.insert(it, 2);  // 2 0
  13.    myList.insert(it,2,20);  // <--- Compile errors  
  14. }
  15.  
==================
%g++ simpleList.cc
*************************
/software/gnu/gcc/2.8.1/sun4os5.10/include/g++/stl_list.h: In method `void list<int,__default_alloc_template<false,0> >::insert(struct __list_iterator<int,int &,int *>, int, int)':
/software/gnu/gcc/2.8.1/sun4os5.10/include/g++/stl_list.h:387: invalid type argument of `unary *'
*************************

Why does this fail to compile? Any help would be appreciated.

thanks,

PL
Oct 14 '11 #1

✓ answered by Banfa

Your second listing compiles without error without alteration for me.

From what you've posted it looks like you are using v2.8.1 of the gcc compiler which is somewhat out of date, I am using version 4.4

invalid type argument of `unary *' means the the * (dereferencing) operator is being applied to a type that doesn't support it, for example this code produces this error at line 4

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     int a = 600;
  4.     int b = *a;
  5.     return 0;
  6. }
  7.  
Are those the only errors your compilation is producing? I suggest you examine the code in the stl_list.h to see exactly what it is doing.

4 2501
Banfa
9,065 Expert Mod 8TB
Once I fixed the main return type to int and added std:: to the fron of the declarations on lines 6 and 7 this compiles fine for me.
Oct 14 '11 #2
I tried your suggestions, but still failed:

============
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.    std::list<int> myList;
  9.    std::list<int>::iterator it;
  10.  
  11.    myList.push_back(0);
  12.    it = myList.begin();
  13.  
  14.    myList.insert(it, 2);  // 2 0
  15.    myList.insert(it,2,20);  // <--- Compile errors
  16.    return 0;
  17.  
  18. }
  19.  
**********************
g++ listQuestion.cc
/software/gnu/gcc/2.8.1/sun4os5.10/include/g++/stl_list.h: In method `void list<int,__default_alloc_template<false,0> >::insert(struct __list_iterator<int,int &,int *>, int, int)':
/software/gnu/gcc/2.8.1/sun4os5.10/include/g++/stl_list.h:387: invalid type argument of `unary *'
Oct 15 '11 #3
Banfa
9,065 Expert Mod 8TB
Your second listing compiles without error without alteration for me.

From what you've posted it looks like you are using v2.8.1 of the gcc compiler which is somewhat out of date, I am using version 4.4

invalid type argument of `unary *' means the the * (dereferencing) operator is being applied to a type that doesn't support it, for example this code produces this error at line 4

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     int a = 600;
  4.     int b = *a;
  5.     return 0;
  6. }
  7.  
Are those the only errors your compilation is producing? I suggest you examine the code in the stl_list.h to see exactly what it is doing.
Oct 17 '11 #4
Thanks, Banfa. I updated my compiler to a newer version, and it's compiling fine now.

Regards,
Oct 18 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: hoox2 | last post by:
void push_front(Node const*& head, int data); Can someone tell me what it means for "const*&"? A reference or a pointer?
12
by: zealotcat | last post by:
template <class T> inline T const& max (T const& a, T const& b) { // if a < b then use b else use a return a<b?b:a; } thanks very much!!
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
6
by: p|OtrEk | last post by:
What is practic difference between this two declarations? If i want call my func with func("blah") i could write: 1) func(std::string const& arg1) 2) func(const std::string& arg1) Whats better to...
0
by: tom olson | last post by:
After more searching I found that defining const operators can cause problems with many compilers due to the way it interprets the C++ standard. I removed the const operators from my class and it...
8
by: vsgdp | last post by:
Does this mean (T* const)& (i.e., a reference to a constant pointer to a T). So the pointer is constant but not the object pointed to?
9
by: miaohua1982 | last post by:
the program is as follows: #include <vector> using namespace std; class A{}; int main() { A* const &p = NULL; vector<A*B(3,NULL); //there is a compile error B.push_back(NULL);
13
by: dragoncoder | last post by:
Hi everyone, please consider the following function:- const int& foo ( const double& d ) { return d; } g++ compiles it with warnings and solaris CC gives error. I want to know if the code...
2
by: ek | last post by:
This first example does not work (cannot be overloaded): int& operator()(int a) { // (1) return a; } int const& operator()(int a) { // (2) return a; }
2
by: nassim.bouayad.agha | last post by:
Hello, here is a code snippet showning my problem : template<typename _K> class TClass1 { public: void Process(const _K& arg) const {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.