?
Solved

PLEASE HELP: Problem with my OO program

Posted on 2003-11-06
2
Medium Priority
?
513 Views
Last Modified: 2010-05-18
I am quite new to the concept of OO programming.  I am having trouble with my code, and not sure how to correct it.  I'll be very very gratefull if you can help me.

My code is supposed to simulate two sets of traffic lights in a traffic junction.  There are two classes "trafficJunction" and "trafficLight".  "trafficJunction" is in charge of controlling the traffic lights and "trafficLight" contains the attributes of traffic lights.

The problem starts when I create two objects of type trafficLight in trafficJunction.  These objects are NS_bound and EW_bound (look in the header file for trafficJunction below).

This quiestion is a little long, but please help me.

below is the header file for trafficJunction

    [source]
[i]
#include <iostream>
#include "trafficLight.h"
using namespace std;


class trafficJunction {
private:
  char Name1[20];
  char Name2[20];
  trafficLight* NS_bound ;
  trafficLight* EW_bound;

 public:
  trafficJunction(char T1Name[], char T2Name[]);
  void carWantsToCross(char side);
  void change_to_green(char bound);
  void change_to_red(char bound);

};
[/source]
[/i]


This is the implementation file for it
[source]
[i]      
#include <iostream>
#include "trafficJunction.h"

using namespace std;

trafficJunction::trafficJunction(char T1Name[], char T2Name[])
{
  strcpy(Name1,T1Name);
  strcpy(Name2,T2Name);
  NS_bound = new trafficLight(Name1, 1);
  EW_bound = new trafficLight(Name2, 1);

}


void trafficJunction::carWantsToCross(char side)
{
  if ( (side == 'N') || (side == 'S') )
    if (NS_bound.colour == 1) change_to_green('N');
  else {
    if (EW_bound.colour == 1) change_to_green('E');
  }
}


void trafficJunction::change_to_green(char bound) {
  switch (bound)
    {
    case 'N':
      {
      if(EW_bound.colour == 1) {
        NS_bound.change_colour(2);
        NS_bound.change_colour(3);
      }
      else if (EW_bound.colour == 3) change_to_red("EW");
      else NS_bound.change_colour(3);
      }
      break;

    case 'E':
      {
      if(NS_bound.colour == 1) {
        EW_bound.change_colour(2);
        EW_bound.change_colour(3);
      }
      else if (NS_bound.colour == 3) change_to_red('N');
      else EW_bound.change_colour(3);
      }
      break;
      }
}


void trafficJunction::change_to_red(char bound) {
  switch(bound)
    {
    case 'N':
      {
      if(NS_bound.colour == 3) {
        NS_bound.colour(2);
        change_to_green('E');
      }

      if(NS_bound.colour == 2) {
        NS_bound.change_colour(1);
        change_to_green('E');
      }
      } break;

    case 'E':
      {
      if(EW_bound.colour == 3) {
        EW_bound.colour(2);
        change_to_green('N');
      }

      if(EW_bound.colour == 2) {
        EW_bound.change_colour(1);
        change_to_green('N');
      }
      } break;
    }
}
[/i]
[/source]

for your information these are the trafficLight's header and implementation files:
[i]
[source]      
#include <iostream>
using namespace std;

class trafficLight {
  private:
  char name[20];
  int colour;
 
 public:

  trafficLight(char TLname[], int TLcolour);
  void change_colour(int Tcolour);
};
[/i]
[/source]

trafficLight.cpp
 [source]
[i]    
#include <iostream>
#include "trafficLight.h"

using namespace std;


trafficLight::trafficLight(char TLname[], int TLcolour)
{
  strcpy(name,TLname);
  colour = TLcolour;
}


void trafficLight::change_colour(int Tcolour)
{
  colour = Tcolour;
}
[/i]
[/source]


When I compile I get the below errors
[code]
[i]
$ make
g++ -g -c trafficJunction.cpp
trafficJunction.cpp: In method `void trafficJunction::carWantsToCross(char)':
trafficJunction.cpp:26: request for member `colour' in `trafficJunction::NS_boun
d', which is of non-aggregate type `trafficLight *'
trafficJunction.cpp:28: request for member `colour' in `trafficJunction::EW_boun
d', which is of non-aggregate type `trafficLight *'
trafficJunction.cpp: In method `void trafficJunction::change_to_green(char)':
trafficJunction.cpp:38: request for member `colour' in `trafficJunction::EW_boun
d', which is of non-aggregate type `trafficLight *'
trafficJunction.cpp:39: request for member `change_colour' in `trafficJunction::
NS_bound', which is of non-aggregate type `trafficLight *'
trafficJunction.cpp:40: request for member `change_colour' in `trafficJunction::
NS_bound', which is of non-aggregate type `trafficLight *'
trafficJunction.cpp:42: request for member `colour' in `trafficJunction::EW_boun
d', which is of non-aggregate type `trafficLight *'
trafficJunction.cpp:42: no matching function for call to `trafficJunction::chang
e_to_red (const char[3])'
trafficJunction.h:22: candidates are: void trafficJunction::change_to_red(char)
trafficJunction.cpp:43: request for member `change_colour' in `trafficJunction::
NS_bound', which is of non-aggregate type `trafficLight *'
trafficJunction.cpp:49: request for member `colour' in `trafficJunction::NS_boun
d', which is of non-aggregate type `trafficLight *'
trafficJunction.cpp:50: request for member `change_colour' in `trafficJunction::
EW_bound', which is of non-aggregate type `trafficLight *'
trafficJunction.cpp:51: request for member `change_colour' in `trafficJunction::
EW_bound', which is of non-aggregate type `trafficLight *'
trafficJunction.cpp:53: request for member `colour' in `trafficJunction::NS_boun
d', which is of non-aggregate type `trafficLight *'
trafficJunction.cpp:54: request for member `change_colour' in `trafficJunction::
EW_bound', which is of non-aggregate type `trafficLight *'
trafficJunction.cpp: In method `void trafficJunction::change_to_red(char)':
trafficJunction.cpp:66: request for member `colour' in `trafficJunction::NS_boun
d', which is of non-aggregate type `trafficLight *'
trafficJunction.cpp:67: request for member `colour' in `trafficJunction::NS_boun
d', which is of non-aggregate type `trafficLight *'
trafficJunction.cpp:71: request for member `colour' in `trafficJunction::NS_boun
d', which is of non-aggregate type `trafficLight *'
trafficJunction.cpp:72: request for member `change_colour' in `trafficJunction::
NS_bound', which is of non-aggregate type `trafficLight *'
trafficJunction.cpp:79: request for member `colour' in `trafficJunction::EW_boun
d', which is of non-aggregate type `trafficLight *'
trafficJunction.cpp:80: request for member `colour' in `trafficJunction::EW_boun
d', which is of non-aggregate type `trafficLight *'
trafficJunction.cpp:84: request for member `colour' in `trafficJunction::EW_boun
d', which is of non-aggregate type `trafficLight *'
trafficJunction.cpp:85: request for member `change_colour' in `trafficJunction::
EW_bound', which is of non-aggregate type `trafficLight *'
make: *** [trafficJunction.o] Error 1
[/i]  
[/code]  

=============================================
I did try to set 'colour' in trafficLight to public but it made no difference
=============================================

Sorry that this post is long, but I'll be gratefull if you can help me.

Thanx in advance
0
Comment
Question by:espadana
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Accepted Solution

by:
efn earned 1000 total points
ID: 9697726
NS_bound and EW_bound are pointers, so they don't have colour members themselves.  You have to dereference the pointers to get to the trafficLight objects, which do have colour members.  So you need an expression like this:

(*NS_bound).colour

This kind of referencing is so common that the language provides a simpler notation for it:

NS_bound->colour

--efn
0
 

Author Comment

by:espadana
ID: 9697863
Thank you very much.  It works fine now
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question