const字符串的getter说未定义但它是?

时间:2015-04-18 02:08:32

标签: c++ static const getter derived-class

我在这里有一个奇怪的情况,无法弄明白。我试图在每个类中创建一个静态const字符串变量并使用getter检索它,但是我收到了错误消息:

| 39 |对商业::类型' |

的未定义引用

我有一个包含大量文件的非常大的项目,所以我会发布我遇到问题的部分,希望这已经足够了。

Commercial.h文件:

#ifndef __COMMERCIAL_H__
#define __COMMERCIAL_H__


#include "property.h"

class Commercial :  public virtual Property
{
private:
  static const string type;

protected:
  string license;

public:

  Commercial();
  Commercial(string theOwner, string theAddress, string theSuburb,
              int thepostCode, string theLicense);

   ~Commercial() ;

   Commercial(const Commercial& orig);

  void input() ;   // Data input for a Shop object
  void print() ;  // Data output for a Shop object
  bool isProperty(Property& other);

  string getLicense() const {return license;};   //Note the use of const

  void setLicense(string theLicense) {license = theLicense;};

  const string getType() const {return type;};

  static void setType() {const string type = "Commercial";};
};

#endif

Commercial.cpp文件:

#include "commercial.h"


Commercial::Commercial() : Property() {
   owner = "NULL";
   address = "NULL";
   suburb = "NULL";
   postcode = 0;
   license = "NULL";
}

Commercial::Commercial(string theOwner, string theAddress,
                        string theSuburb, int thepostCode,
                        string theLicense): Property(theOwner, theAddress,
                        theSuburb, thepostCode), license(theLicense) {}

Commercial::~Commercial() {}

Commercial::Commercial(const Commercial& orig) : Property(orig),
                        license(orig.getLicense()) {}

void Commercial::print() {
   cout << getOwner() << endl;
   cout << getAddress() << endl;
   cout << getSuburb() << endl;
   cout << getPostcode() << endl;
   cout << getLicense() << endl;
}

void Commercial::input() {

   string tempOwner;
   string tempAddress;
   string tempSuburb;
   int tempPostcode;
   string tempLicense;

   cout << endl;

   do {
      cout << "Enter The Owner: ";
      getline(cin, tempOwner, '\n');
   } while (cin.fail());

   do {
      cout << "Enter The Address: ";
      getline(cin, tempAddress, '\n');
   } while (cin.fail());

   do {
      cout << "Enter The Suburb: ";
      getline(cin, tempSuburb, '\n');
   } while (cin.fail());

   for (;;) {
      cout << "Enter The Postcode: ";
      cin >> tempPostcode;
      if ((cin)) {
         clearInputBuffer();
         break;
      }
      cin.clear();
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
   }

   do {
      cout << "Enter The License: ";
      getline(cin, tempLicense, '\n');
   } while (cin.fail());

   setOwner(tempOwner);
   setAddress(tempAddress);
   setSuburb(tempSuburb);
   setPostcode(tempPostcode);
   setLicense(tempLicense);
}

bool Commercial::isProperty(Property& other) {

   if (typeid(other) == typeid(Commercial)) {
      return true;
   }

   return false;
}

property.h文件:

#ifndef __PROPERTY_H__
#define __PROPERTY_H__


/*TODO  REQUIRED HEADER FILES AND NAMESPACES*/
#include <string>
#include "utility1.h"

class Property
{
private:
  static const string type;

protected:
  string owner;
  string address;
  string suburb;
  int postcode;

public:
  Property();
  Property(string theOwner, string theAddress,
           string theSuburb, int thepostCode);
  virtual ~Property();
  Property(const Property& orig);
  virtual void input() = 0;   // Data input for a Property object
  virtual void print() = 0;  // Data output for a Property object
  virtual bool isProperty(Property& other) = 0;

  string getOwner() const {return owner;};   //Note the use of const
  string getAddress() const {return address;};
  string getSuburb() const {return suburb;};
  int getPostcode() const {return postcode;};

  void setOwner(string newOwner) {owner = newOwner;};
  void setAddress(string newAddress) {address = newAddress;};
  void setSuburb( string  newSuburb) {suburb = newSuburb;};
  void setPostcode(int  newPostcode) {postcode = newPostcode;};

  virtual const string getType() const {return type;};

// Un-needed and pointless, just done to make the compiler happy.
  static void setType() {const string type = "Property";};
};
#endif

Property.c文件:

#include "property.h"

Property::Property(){
   owner = "NULL";
   address = "NULL";
   suburb = "NULL";
   postcode = 0;
}

Property::Property(string theOwner, string theAddress,
                   string theSuburb, int thepostCode):
                     owner(theOwner), address(theAddress),
                     suburb(theSuburb), postcode(thepostCode){}

Property::~Property() {}

Property::Property(const Property& orig) :
                     owner(orig.getOwner()), address(orig.getAddress()),
                     suburb(orig.getSuburb()), postcode(getPostcode()) {}

void Property::print() {
   cout << getOwner() << endl;
   cout << getAddress() << endl;
   cout << getSuburb() << endl;
   cout << getPostcode() << endl;
}

void Property::input() {

   string tempOwner;
   string tempAddress;
   string tempSuburb;
   int tempPostcode;

   cout << endl;

   do {
      cout << "Enter The Owner: ";
      getline(cin, tempOwner, '\n');
   } while (cin.fail());

   do {
      cout << "Enter The Address: ";
      getline(cin, tempAddress, '\n');
   } while (cin.fail());

   do {
      cout << "Enter The Suburb: ";
      getline(cin, tempSuburb, '\n');
   } while (cin.fail());

   for (;;) {
      cout << "Enter The Postcode: ";
      cin >> tempPostcode;
      if ((cin)) {
         clearInputBuffer();
         break;
      }
      cin.clear();
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
   }

   setOwner(tempOwner);
   setAddress(tempAddress);
   setSuburb(tempSuburb);
   setPostcode(tempPostcode);
}

测试/驱动程序.cpp文件:

#include "property.h"
#include "utility1.h"
#include "rentals.h"
#include "commercial.h"
#include "sales.h"
#include "comSales.h"
#include "resSales.h"
#include "resRentals.h"
#include "comRentals.h"

void testingNewFunction(Property* properties[]);

const int MAX_PROPERTIES = 5;

int main(void) {
   int typeCount = 0;

   Property *properties[MAX_PROPERTIES];

   properties[0] = new Commercial("Notting Hill McDonalds", "4 Gardiner Road",
                                  "Notting Hill", 5000, "Li3000");

   properties[1] = new ResRentals("Janet Dalgleish", "30 Firhill Court",
                           "Mary Hill", 4000, 500.00, 300.00, 4);

   properties[2] = new Commercial();
   properties[2] = properties[0];

   properties[3] = new ComSales();
   properties[3]->input();

   properties[4] = new ComRentals();
   properties[4]->setOwner("River Kwai Restaurant");
   properties[4]->setAddress("3 Bishopton Road");
   properties[4]->setSuburb("Footscray");
   properties[4]->setPostcode(5000);
   dynamic_cast<Commercial*>(properties[4])->setLicense("L8888");
   dynamic_cast<Rentals*>(properties[4])->setBond(10000);
   dynamic_cast<Rentals*>(properties[4])->setMonthlyRent(700);


   cout << endl;

   for (int i = 0; i < MAX_PROPERTIES; i++) {

      ComSales* tempType = new ComSales();

      if (properties[i]->isProperty(*dynamic_cast<ComSales*>(tempType))) {
         typeCount++;
      }
      if (i == MAX_PROPERTIES-1) {
         cout << typeCount << " ComSales" << endl;
         typeCount = 0;
      }
   }


   for (int i = 0; i < MAX_PROPERTIES; i++) {

      ComRentals* tempType = new ComRentals();

      if (properties[i]->isProperty(*dynamic_cast<ComRentals*>(tempType))) {
         typeCount++;
      }
      if (i == MAX_PROPERTIES-1) {
         cout << typeCount << " ComRentals" << endl;
         typeCount = 0;
      }
   }


   for (int i = 0; i < MAX_PROPERTIES; i++) {

      ResSales* tempType = new ResSales();

      if (properties[i]->isProperty(*dynamic_cast<ResSales*>(tempType))) {
         typeCount++;
      }
      if (i == MAX_PROPERTIES-1) {
         cout << typeCount << " ResSales" << endl;
         typeCount = 0;
      }
   }


   for (int i = 0; i < MAX_PROPERTIES; i++) {
      cout << properties[i]->getOwner() << " is a "
      << typeid(*properties[i]).name() << endl;
   }


   testingNewFunction(&properties[MAX_PROPERTIES]);

   return 0;
}

void testingNewFunction(Property* properties[]) {

   // Initializing static variables (normally would be done in a separate
   // function).
   Commercial::setType();
   ComRentals::setType();
   ComSales::setType();
   Property::setType();
   Rentals::setType();
   Residential::setType();
   ResRentals::setType();
   ResSales::setType();
   Sales::setType();

   int comRentalsCount = 0;
   int comSalesCount = 0;
   int ResSalesCount = 0;

   for (int i = 0; i < MAX_PROPERTIES; i++) {
      const string tempType = properties[i]->getType(); //<--- trying to access the type string for each property in the array
                                                        // which is saying the reference is undefined.
   }


}

布局很奇怪,因为它来自学校作业,我正在乱搞测试用它做事的新方法。请帮忙:)

编辑:这个问题被标记为重复,但是问题表明这是一个副本,没有帮助做任何与此有关,因为它是关于链接错误,我无法看到任何与此有关在我的问题上,如果是,请具体说明原因。

提前致谢。

0 个答案:

没有答案