【问题标题】:C++ classes query error : left of "" must point to class/struct/unionC++ 类查询错误:“”左侧必须指向类/结构/联合
【发布时间】:2014-11-17 15:19:50
【问题描述】:

我正在尝试实现一个名为 Person 的类,该类由以下成员组成:name、gender 和 ads,其中 ads 属于名为 Address 的类类型,由成员 street、tel 和 PObox 组成。当我编译程序时,每当我尝试在任何 Person 实现中调用广告时都会出现错误(尽管语法应该是正确的)。我附上了 address.h , person.h ,Person 的实现和地址文件的实现。

address.h

#include<iostream>
class Address
{
public:
    Address();
    char* getstreet();
    char* gettel();
    int getpobox();
    void setall(char *str , char *tel , int pobox);
    void print();
    /*~Address();*/

private:
    char* street;
    char* tel;
    int POBox; 
};

person.h

#include <iostream>
#include "address.h" // header file for the Address class
#include "gender.h" // header file for the Gender enum 
using namespace std;
class Person
{
  public:
    Person();
    Person(char *n, Gender *g, Address *ad);
    Person(const Person &f);
    void setName(char * n);
    void setAds( Address *ad); 
    char *getName();
    /*Address *getAds();*/
    /*~Person();*/
    void print();

private:
    char *name;
    Gender *gender; 
    Address *ads;
};

实施 2.cpp

#include<iostream>
#ifndef address_h
#define address_h
#include"address.h"
using namespace std;
//IMPLEMENTATION OF ADDRESS FUNCTIONS

Address::Address()
    {
        street = "default street";
        tel = "55555555";
        POBox = 1315425;
    }
    char* Address::getstreet()
    {
        return street;
    }
    char* Address::gettel()
    {
        return tel;
    }
    int Address::getpobox()
    {
        return POBox;
    }
    void Address::setall(char *str , char *tel2 , int pobox)
    {
    street = str;
    tel = tel2;
    POBox = pobox;
    }
    void Address::print()
    {
        cout<<"Street : "<<street<<endl;
        cout<<"Telephone : "<<tel<<endl;
        cout<<"PO box : "<<POBox<<endl;
    }

#endif

实施.cpp

#ifndef person_h
#define person_h
#include<string>
#include<iostream>
#include"address.h"
#include"person.h"
using namespace std;
    //IMPLEMENTATION OF PERSON FUNCTIONS
    Person::Person()
    {
        (*gender) = female;
        name = "testname";
        (*ads).setall("random adress","0503216532",95421);
    }
    Person::Person(char *n, Gender *g, Address *ad) //copy constructor
    {
        n = name;
        g = gender;
        ad->setall("cpyconstruct test","42324134",14925);
    }
    Person::Person(const Person &f)
    {
        name = f.name;
        gender = f.gender;
        ads = f.ads;
    }
    void Person:: setName(char * n)
    {
        n = "karim (TESTING SETNAME)";
    }
    void Person::setAds(Address *ad)
    {
        ads->setall("aus","04314013",14314);
    }
    char* Person::getName()
    {
        return name;
    }
    void Person:: print()
    {
        cout<<"Name : "<<name<<endl;
        if(gender == 0)
        {
        cout<<"gender : male"<<endl;
        }
        else
        cout<<"gender : female"<<endl;
        ads->setall("hello","056323453",1995);
    }

#endif

【问题讨论】:

  • 错误说明这是在哪一行?
  • 我尝试对广告做任何事情的所有行
  • 真的需要使用std::string!否则,您应该使用char [] 或使用new 进行分配(但使用std::string!)
  • 我应该在哪里分配新的,为什么? @crashmstr
  • @KareemYoussef 您将字符串文字分配给char *,这不是一个好主意。相反,您应该使用std::string 并让标准库完成繁重的工作。否则,您应该使用或分配 new 和字符数组并使用 strcpy 等。人。对于 C 字符串。

标签: c++ class pointers setter


【解决方案1】:

ads 是一个未初始化的指针,所以你不能用它做任何事情(比如,使用 -> 操作符)。

解决方法:在 Person 的 ctor 中用 new 初始化;或者,更好的是,根本不使用指针。你不需要一个。

class Person
{ 
  ...
  Address ads;
};

Person::Person()
{
    (*gender) = female;
    name = "testname";
    ads.setall("random adress","0503216532",95421);
}

...crashmaster 说得对:当你让ads 工作时,你会遇到 Address 成员的内存问题。使用 std::string 他们也会得到照顾。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多