LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-18-2010, 05:22 AM   #1
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Rep: Reputation: 16
Passing class to itself (CPP)


Hi,

I'm struggling with the issue of passing a vector of a class to itself, here's what state its in now... (tried many variations, but without direction).

Code:
#include <iostream>
#include <string>
#include <vector>

//	g++ classes.cpp -o run

using namespace std;

class node {
	public:
		string name;
		node()	{	name = "test";	}
		int check(vector <node*> *n)
		{
			int tot = n->size();
			for(int i=0;i<tot;i++) {
				cout << "name: " << n[i]->name << endl;
			}
		}
};

int main(int argc, char* argv[])
{
	std::vector <node*> e;
	
	node *n = new node();
	
	e.push_back(n);
	e.push_back(new node());
	e.push_back(new node());
	
	int ret e[0]->check(&e);
	return 0;

}
I'm guessing it will be possible, but... Any help or direction greatly appreciated, so would a college (uni) course... Bang bang...
 
Old 05-18-2010, 05:43 AM   #2
posixculprit
Member
 
Registered: May 2010
Posts: 136

Rep: Reputation: 42
Code:
int check(vector <node*> *n)
`n` is a pointer to a std::vector<node*>. Because of this, you can't access its elements as n[i], you have to dereference it first: (*n)[i]. However, why use a pointer in the first place? I'm guessing it is because you wish to not copy the whole vector. A better way (IMO) would be:

Code:
int check(const vector<node *> &n)
Also, even though you've declared `check` to return int, the function returns no value. Also, why is `check` a member of class `node` in the first place? You're also missing an equals sign on the line:

Code:
int ret e[0]->check(&e);
 
Old 05-18-2010, 05:53 AM   #3
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Original Poster
Rep: Reputation: 16
Thanks, i'll have a read up on dereferencing and pointers in general.

The obvious errors are because I quickly made this example to demonstrate my issue, err...

Once again, many thanks!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
passing class references in gtk (C++) jiml8 Programming 11 01-07-2009 10:02 AM
Problem with passing a class address to a thread nalsrayatko Programming 2 11-25-2007 11:59 AM
CPP: Question about memory consumption of many vitural functions in a class of mine. RHLinuxGUY Programming 3 04-30-2007 11:01 PM
CPP: Is it possible to use the members of an inherited class? RHLinuxGUY Programming 5 04-30-2007 02:48 AM
passing a class member function to pthread_create. dmail Programming 1 07-29-2006 11:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration