Hi at this part of the code at the default constructor

codedateInception = 0;
dateDeath = NULL:
location = 0;

I get errors and I know its because they are of a class. I don't know how to set it up to have values. Any help?


Header File

#ifndef ACTOR_H_
#define ACTOR_H_

#include "Point3D.h"
#include "Date.h"
#include <iostream>

using namespace std;

class Actor{
public:
    Actor();
    string getName() const;
    double getMaxSpeed() const;
    int getWeight() const;
    int getHeight() const;
    double getTendency() const;
    Date getInception() const;
    Date getDeath() const;
    Point3D getLocation() const;
    bool validityWeight(int testWeight);
    bool validityHeight(int testHeight);
    bool validityTendency(int testTendency);
    string getCharacteristics();

private:
    string name;
    double maxSpeed;
    double tendency;
    int height;
    int weight;
    Date dateInception;
    Date dateDeath;
    Point3D location;
    bool test;
};
#endif /* ACTOR_H_ */

Actor Method file

#include "Actor.h"
#include "Point3D.h"
#include "Date.h"
#include <iostream>
#include <sstream>

using namespace std;

Actor::Actor(){
	test = false;
	name = "unknown";
	maxSpeed = 0;
	height = 0;
	weight = 0;
	tendency = 0;
	dateInception = 0;
	dateDeath = NULL:
	location = 0;

}

//returns name of type string

string Actor::getName() const{
	return name;
}

//returns maxSpeed of type double

double Actor::getMaxSpeed() const{
	return maxSpeed;
}

//returns weight of type integer

int Actor::getWeight() const{
	return weight;
}

//returns height of type integer

int Actor::getHeight() const{
	return height;
}

//returns tendency of type double

double Actor::getTendency() const{
	return tendency;
}

//returns dateInception of type Date

Date Actor::getInception() const{
	return dateInception;
}

//returns dateDeath of type Date

Date Actor::getDeath() const{
	return dateDeath;
}

//returns location of type Point3D

Point3D Actor::getLocation() const{
	return location;
}

//tests and sets the value of the individual's tendency

bool Actor::validityTendency(int testTendency){
	if(testTendency == -1 || testTendency == 1){
		tendency = testTendency;
		test = true;
	}
	else
		test = false;
	return test;
}

//tests and sets the value of the individual's height

bool Actor::validityHeight(int testHeight){
	if(testHeight <= 1000 && testHeight >= 0){
		height = testHeight;
		test = true;
	}
	else
		test = false;
	return test;
}

//test and sets the value of the individual's weight

bool Actor::validityWeight(int testWeight){
	if(testWeight <= 1000 && testWeight >= 0){
		weight = testWeight;
		test = true;
	}
	else
		test = false;
	return test;
}
string Actor::getCharacteristics(){

	//Uses get function to return the values of Actor members

	tendency = getTendency();
	height = getHeight();
	weight = getWeight();
	maxSpeed = getMaxSpeed();
	name = getName();
	dateInception = getInception();
	dateDeath = getDeath();
	location = getLocation();
	string brackets = "/";

	//Opens a stringstream and allows integerss to be displayed as a string

	stringstream out;

	string dateString;

	//stores integers and strings into a string out stream

	out << name << brackets << height << brackets << weight << brackets << tendency;

	//displays the string using the command out instead of cout

	out >> dateString;

	return dateString;
}

Recommended Answers

All 2 Replies

Now, the problem is the difference between an object and a pointer to an object. Think of a class as any other type (like int or float or double), if you declare a variable with * it's a pointer to an object, if you declare with & it's a reference to an object, if you declare with neither then it is the object and cannot be set to NULL or any other "invalid" or "empty" value and has to be initialized by one of its constructors.

There are many little things to rectify, see comments in the code (and please post in proper code tags in the future):

#ifndef ACTOR_H_
#define ACTOR_H_

#include "Point3D.h"
#include "Date.h"
#include <iostream>

//using namespace std; //Don't import namespaces in header files!

class Actor {
  public:
    Actor();
    //HERE: you should inline trivial functions -> more efficient and less code bloating in the cpp file.
    std::string getName() const { return name; };
    double getMaxSpeed() const { return maxSpeed; };
    int getWeight() const { return weight; };
    int getHeight() const { return height; };
    double getTendency() const { return tendency; };
    //HERE Date may be a complicated class, better output a const reference instead (same for Point3D).
    const Date& getInception() const { return dateInception; };
    const Date& getDeath() const { return dateDeath };
    const Point3D& getLocation() const { return location; };

    bool validityWeight(int testWeight);
    bool validityHeight(int testHeight);
    bool validityTendency(int testTendency);
    std::string getCharacteristics();

private:
    std::string name;
    double maxSpeed;
    double tendency;
    int height;
    int weight;
    Date dateInception;
    Date dateDeath;
    Point3D location;
    //bool test; //This data member is useless
};
#endif /* ACTOR_H_ */


Actor Method file //Call it an implementation or source file


#include "Actor.h"
#include "Point3D.h"
#include "Date.h"
#include <iostream>
#include <sstream>

using namespace std; //OK to import a namespace in a cpp file

//use initialization list for trivial stuff like this:
Actor::Actor() : test(false),
                 name("unknown"),
                 maxSpeed(0),
                 height(0),
                 weight(0),
                 tendency(0),
                 dateInception(0),
                 dateDeath(), //HERE dateDeath is of type Date, not a pointer to a Date object, so it is initialized as an object is, either with default or with some other parametrized constructor.
                 location(0) { };


//tests and sets the value of the individual's tendency
bool Actor::validityTendency(int testTendency){
  if(testTendency == -1 || testTendency == 1) {
    tendency = testTendency;
    return true; //HERE: no need for a temporary, better branch out by return.
  } else
    return false;
};

//tests and sets the value of the individual's height
bool Actor::validityHeight(int testHeight) {
  if(testHeight <= 1000 && testHeight >= 0){
    height = testHeight;
    return true;
  } else
    return false;
};

//test and sets the value of the individual's weight
bool Actor::validityWeight(int testWeight){
  if(testWeight <= 1000 && testWeight >= 0){
    weight = testWeight;
    return true;
  } else
    return false;
};

string Actor::getCharacteristics(){

  //Uses get function to return the values of Actor members
  /*
  tendency = getTendency();
  height = getHeight();
  weight = getWeight();
  maxSpeed = getMaxSpeed();
  name = getName();
  dateInception = getInception();
  dateDeath = getDeath();
  location = getLocation();
  */ // All the above calls have absolutely no effect, i.e., all equivalent to "a = a;", it's a waste of the compiler's effort to optimize away all of it.
  
  string brackets = "/";

  //Opens a stringstream and allows integerss to be displayed as a string

  stringstream out;

  string dateString;

  //stores integers and strings into a string out stream

  out << name << brackets << height << brackets << weight << brackets << tendency;

  //displays the string using the command out instead of cout

  out >> dateString;

  return dateString;
};
commented: I don't know how you can answer to such a bad post, but hats off to ya +5

Thanks for the help Mike_2000. The reason I was using the get_variable method in the getCharacteristics() was because it was what the teacher required for the project. I was pretty sure I put the correct coding to display the code but I was in a rush so I probably typed it wrong and didn't preview the post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.