Array of structs Problem

This is a discussion on Array of structs Problem within the C++ Programming forums, part of the General Programming Boards category; I don't think I've learned how to access all of an array of structs (that have pointers) through a member ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    25

    Array of structs Problem

    I don't think I've learned how to access all of an array of structs (that have pointers) through a member function. So, I'm trying to output all of the array's struct pointer to data , but I cant seem to figure out how to make the function call. So, how would I go about fixing this problem? thnx

    main.cpp
    Code:
    #include <iostream>
    #include "func.h"
    using namespace std;
    
    int main(int argc, char * argv[])
    {
    	int n = atoi(argv[1]);
    	utree * forest = new utree[n];
    	
    	forest.printArray(); // NOT the right call :/
    
    	return 0;
    }
    func.h
    Code:
    struct node
    {
    	int data;
    	node * parent;
    	node();
    	node ( int, node * );
    };
    
    class utree 
    {
    public:
    	void printArray(int);
    	int find(int);
    private:
    	node * root;
    };
    func.cpp
    Code:
    #include "func.h"
    #include <iostream>
    using namespace std;
    
    /*Construtors*/
    node::node(){data = -1; parent = NULL;}
    node::node(int n, node * link){data = n; parent = link;}
    
    
    void utree::printArray(int n)
    {
    	for(int i = 0; i < n; i++)
    	{
    		cout << this[i].root->data;
    	}
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    in main
    Code:
    	for(int i = 0; i < n; i++)
    	{
    		forest[i].print();
    	}

    in class
    Code:
    void utree::print()
    {
    	if(root)
    	{
    		cout << root->data;
    	}
    }
    your class is only one member of array - it does not know about over members

    if you want a class that "knows" about collection of trees - you need to write a class
    "forest" that will store the array (or better vector) of trees and will have possibility to printArray

    As a side not - I hope you just skipped when posting required functions like constructor/destructor and operator= of your tree class and not missing them
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    25
    Quote Originally Posted by vart View Post
    your class is only one member of array - it does not know about over members

    if you want a class that "knows" about collection of trees - you need to write a class
    "forest" that will store the array (or better vector) of trees and will have possibility to printArray

    As a side not - I hope you just skipped when posting required functions like constructor/destructor and operator= of your tree class and not missing them
    Ya, I was kinda thinking of how it wasn't going to work, thnx. I didn't skip over those functions heh... I just started writing this I haven't wrote anything for a while :/ so i cant really remember much atm
    Last edited by I BLcK I; 02-23-2008 at 12:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 05:07 AM
  2. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 04:19 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Array of Structs question
    By WaterNut in forum C++ Programming
    Replies: 10
    Last Post: 07-02-2004, 02:58 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21