4 Replies - 560 Views - Last Post: 30 March 2010 - 10:30 AM Rate Topic: -----

#1 Guest_Mack*


Reputation:

Node containing class object

Posted 28 March 2010 - 11:06 PM

I'm trying to implement a red black tree containing customer data nodes rather than generic int nodes. I thought that if I #include "customer.h" in my RedBlack class it would know that data is of type customer. But instead I get hammered with redefinition errors and customer data being of undefined type.

What's the proper approach to having my node data being a class object?

//RedBlack.h
class RedBlack
{
public:
	RedBlack(void);
	~RedBlack(void);
private:
	struct Node {
		customer data; //list data
		Node *next; //pointer to next item in the list
	};

	Node *head; //Pointer to the first node in the list
	int size; //Records the number of nodes in the list

};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//RedBlack.cpp
#include "StdAfx.h"
#include "RedBlack.h"
#include "customer.h"

RedBlack::RedBlack(void)
{
}

RedBlack::~RedBlack(void)
{
}


//customer.h
#include <string>
using namespace std;
class customer
{
public:
	customer(string last, char first, int balance);
	void set_account(int balance);
	string get_name();
	char get_initial();
	int get_account();
	~customer(void);
private:
	string name;//customer's last name
	char initial;//customer's first initial
	int account;//balance owed by customer
};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//customer.cpp
#include "StdAfx.h"
#include "customer.h"

customer::customer(string last, char first, int balance)
{
	name = last;
	initial = first;
	account = balance;
}
void customer::set_account(int balance){
	account = balance;
}
string customer::get_name(){
	return name;	
}
char customer::get_initial(){
	return initial;
}
int customer::get_account(){
	return account;
}
customer::~customer(void)
{
}



Is This A Good Question/Topic? 0

Replies To: Node containing class object

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Node containing class object

Posted 28 March 2010 - 11:21 PM

VS 2008 C++

compiles without error:

#pragma once
#include "customer.h"

class RedBlack
{
public:
	RedBlack(void);
	~RedBlack(void);

private:
        struct Node {
                customer data; //list data
                Node *next; //pointer to next item in the list
        };

        Node *head; //Pointer to the first node in the list
        int size; //Records the number of nodes in the list
};


Was This Post Helpful? 0
  • +
  • -

#3 Guest_Mack*


Reputation:

Re: Node containing class object

Posted 29 March 2010 - 11:01 AM

I'm also using VS 2008, tried that exact code and I still got redefinition errors and undefined type customer. I thought maybe templates were another option, but that added more errors...
Was This Post Helpful? 0

#4 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Node containing class object

Posted 29 March 2010 - 11:06 AM

Did you use:
#pragma once


or header guards:
#ifndef MY_HEADER_H
#define MY_HEADER_H

// code here

#endif


in your RedBlack.h header?
#pragma once
include guards
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Mack*


Reputation:

Re: Node containing class object

Posted 30 March 2010 - 10:30 AM

I tried #pragma once at when I first tried debugging since its there by default. I tried it with and without 'customer.h' but it gives me redefinition errors, or missing type specifier.

I got the same problems with
//#pragma once
#ifndef REDBLACK_H
#define REDBLACK_H
//#include "customer.h"

class RedBlack
{
public:
	RedBlack(void);
//	bool isRed(struct node *root);
	~RedBlack(void);
private:

	struct Node {
		bool red; //True if red, False if black
		customer data(); //customer data
		struct node *next[2]; //pointer to an array where index 0 is left and index 1 is right
	};

	struct tree{
	struct node *root;//pointer to the root
	};
#endif
};


I put it in my cpp instead of in the header to no avail.
Was This Post Helpful? 0

Page 1 of 1