2 Replies - 1870 Views - Last Post: 15 November 2009 - 08:06 PM Rate Topic: -----

#1 t0pher  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-November 09

Simple Vector Class Problem

Posted 15 November 2009 - 07:50 PM

I've simplified this code down alot, but my main problem is upon compiling in terminal:

powerbook-g4:Desktop Home$ g + + lab8-2.c p p -o lab8.out
lab8-2.c p p: In constructor ‘vector_list::vector_list()’:
lab8-2.c p p:26: error: insufficient contextual information to determine type

I get these errors.

"insufficient contextual information to determine type"

comes from the line:

list.push_back(i);

I can't seem to get the vector to work in the constructor. I know that I'm just approaching this all wrong, and any help would be very appreciated.

#include <iostream>
#include <vector>

using namespace std;

class vector_list {

public:
	vector_list() {
		i=10;
		list.push_back(i);
	}
	
	int i;
	vector <int> list();

}; //end class

int main() {
	vector_list myList;
}



When I try to compile the same code in Visual Studio it errors:

\lab8-2.c p p(13) : error C2228: left of '.push_back' must have class/struct/union

Is This A Good Question/Topic? 0
  • +

Replies To: Simple Vector Class Problem

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Simple Vector Class Problem

Posted 15 November 2009 - 07:55 PM

You declared list as a method of the class, not a member variable.
Was This Post Helpful? 0
  • +
  • -

#3 t0pher  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-November 09

Re: Simple Vector Class Problem

Posted 15 November 2009 - 08:06 PM

Cool I got it to work for a second.

Thanks for the help. I'm new at this.

I'd like to create the vector list in the constructor then.. If i get what your saying correctly.
But then I'd like to access it in another function such as Insert() below.
I'm getting the same error where list is an undeclared identifier in the void Insert() function.

class vector_list {

public:
	//CONSTRUCTOR
	vector_list(int c) {
		vector <int> list(c);
	}
	void Insert(int pos, int num) {	//inserts number to list at the given position
		vector<int>::iterator theIterator = list.begin();
		list.insert( theIterator, pos, num );
	}

}; //end class

int main() {
   vector_list myList(3);
}


This post has been edited by t0pher: 15 November 2009 - 08:22 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1