朋友功能,预期的主要表达方式在之前.代币 [英] Friend Function, expected Primary Expression before . token

查看:86
本文介绍了朋友功能,预期的主要表达方式在之前.代币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以在单独的头文件中有两个类

So there are two classes in separate header files

客户.h

using namespace std;
#include <iostream>

class Customer{
    friend void Display();
private:
    int number, zipCode;
public:
    Customer(int N, int Z){
    number = N;
    zipCode = Z;
    }
};

城市.H使用命名空间std;#包括#include"Customer.h"

City. h using namespace std; #include #include "Customer.h"

class City{
    friend void Display();
private:
    int zipCode;
    string city, state;
public:
    City(int Z, string C, string S){
        zipCode = Z;
        city = C;
        state = S;
    }
};

我的main.cpp如下

my main.cpp is as follows

#include "City.h"
#include "Customer.h"

void Display(){
        cout<<"Identification Number: "<<Customer.number<<endl
                <<"Zip Code: "<<Customer.zipCode<<endl
                <<"City: "<<City.city<<endl
                <<"State: "<<City.state<<endl;
    }


int main() {
    Customer A(1222422, 44150);
    City B(44150, "Woklahoma", "Kansas");

    Display();

}

我对c ++的基础很好,但是这是我不明白的地方,所以我的具体问题是....为什么对于Display函数的四行,编译器会告诉我错误:期望的主要对象-在'.'令牌之前的表达式"

I'm good with the basics of c++ but this is where I Don't understand, so my specific question is.... Why for the four lines of my Display function does the compiler tell me "error: expected primary-expression before ‘.’ token"

预先感谢Macaire

Thanks in advance, Macaire

推荐答案

Customer 是一种类型.您需要使用该类型的对象来访问其 number 成员(其余各行相同).

Customer is a type. You need an object of that type to access it's number member (and the same for the rest of the lines).

您可能打算将客户城市用作 Display 的参数:

You probably meant to take a Customer and City as arguments to Display:

void Display(Customer customer, City city){
    cout<<"Identification Number: "<<customer.number<<endl
            <<"Zip Code: "<<customer.zipCode<<endl
            <<"City: "<<city.city<<endl
            <<"State: "<<city.state<<endl;
}

然后将您的 Customer City 对象传递给该函数:

Then pass your Customer and City objects to that function:

Display(A, B);

这篇关于朋友功能,预期的主要表达方式在之前.代币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆