首页 > 解决方案 > 如何从另一个头文件c ++调用函数

问题描述

我的大部分工作都在努力解决employee.h 上的语法错误

这是所有 3 个标题

地址.h

#pragma once
#include <string>
using namespace std;

class Address
{
public:
    explicit Address();
    explicit Address(const string& city, const string& state,
        const string& street, const string& zip);
    const string& getCity() const;
    const string& getState() const;
    const string&  getStreet() const;
    const string&  getZip() const;
    void printAddress() const;
private:
    string street;
    string city;
    string state;
    string zip;
};

Address::Address() :
    city("Beverly Hills,"),
    state("CA,"),
    street("99999 Sunset Boulevard,"),
    zip("99999")
{ }

Address::Address(const string& city, const string& state,
    const string& street, const string& zip) :
    city(city), state(state), street(street), zip(zip)
{ }

const string& Address::getCity() const
{
    return city;
}
const string& Address::getState() const
{
    return state;
}
const string& Address::getStreet() const
{
    return street;
}
const string& Address::getZip() const
{
    return zip;
}
void Address::printAddress() const
{
    std::cout << street << city << state << zip << endl;
};

名称.h

#include <string>
using namespace std;

class Name
{
public:
    explicit Name();
    explicit Name(const string& firstName, const string& middleName, const string& lastName);
    const string& getFirstLast() const;
    void printName() const;
private:
    string firstName;
    string middleName;
    string lastName;

    Name::Name() :
        firstName("John"),
        middleName("H."),
        lastName("Doe")
    {}
    Name::Name(const string& firstName, const string& middleName, const string& lastName) :
        firstName(firstName), lastName(lastName)
    { }
    const string& Name::getFirstLast() const
    {
        return name;
    }

    void Name::printName() const
    {
        std::cout << firstName << middleName << lastName << endl;


    }
};

雇员.H

这是我得到大部分错误的地方。

#include <iostream>
#include <string>
#include "Name.h"
#include "Address.h"
using namespace std;

class Employee
{
public:
    explicit Employee();
    explicit Employee(const Name& name, const Address& address, const string& ssn);
    const string& getName() const;
    const string& getSSN() const;
    const string& getAddress() const;
    void printEmployee() const;
private:
    Name name;
    Address address;
    string ssn;
};
Employee::Employee() :
    name("John H. Doe"),
    address("99999 Sunset Boulevard", "Beverly Hills", "CA", "99999"),
    SSN("999-99-9999")
{}
Employee::Employee(const Name& name, const Address& address, const std::string& ssn) :
    name(name), address(address), ssn(ssn)
{ }

const string& Employee::getName() const
{
    return printName;
}

const string& Employee::getSSN() const
{
    return ssn;
}
const string& Employee::getAddress() const
{
    return address
}


void Employee::printEmployee() const
{
cout << Name.printName() << Address.printAddress() << ssn << std::endl;
}

以下是作业说明

名称头文件 (Name.h) 将具有:

默认构造函数。请记住,Name 的默认构造函数具有以下初始值: Name 为“John H. Doe”。

具有 3 个参数的构造函数:一个用于名字,一个用于中间名,一个用于姓氏。

3 个私有字符串实例变量:名字、中间名和姓氏。

getFirstLast() 函数:它按顺序返回名字和中间名和姓氏

printName() 函数:它打印名字、中间名和姓氏。

地址头文件 (Address.h) 将具有:

默认构造函数。请记住,Address 的默认构造函数具有以下初始值:Address to "99999 Sunset Boulevard"、"Beverly Hills"、"CA"、"99999"

4 个私有字符串实例变量,用于:Street、City、State、Zip

一个有 4 个参数的构造函数:一个用于街道,一个用于城市,一个用于州,一个用于邮政编码。

getCity():返回城市

getState():返回状态

getStreet():返回街道

getZip():它返回 zip

printAddress():它打印街道、城市、州和邮编。

员工头文件 (Employee.h) 将具有:

3 个私有实例变量:一个用于名称(使用上面的标题名称),一个用于地址(使用上面的地址标题),一个用于 SSN 的字符串变量。

将 SSN 初始化为“999-99-9999”的默认构造函数,将名称初始化为“John H. Doe”,将地址初始化为“99999 Sunset Boulevard”、“Beverly Hills”、“CA”、“99999”

具有 3 个参数的构造函数:一个用于名称,一个用于地址,一个用于 SSN 的字符串。

getName() 函数:它返回员工的姓名

getAddress() 函数:它返回 Employee 的地址。

getSSN() 函数:它将 SSN 作为字符串返回

printEmployee() 函数:

打印名称:确保使用 Name.h 中的 printName() 函数

打印地址:确保使用 Address.h 中的 printAddress() 函数

打印 SSN。

员工 (Employee.cpp) 类将具有:

在 void main() 函数中,您将声明:

名称 n;

地址A;

和雇员 e;

并使用 printEmployee() 打印 e。

您还需要声明:

名字 n1:你的名字

一个地址 a1:你自己的地址

字符串 ssn1:987-65-4321

员工 e1,其名称为 n1,地址为 A1,ssn1。

使用 printEmployee() 打印 e1。

标签: c++data-structures

解决方案


我试图为您创建一个完整的示例。您应该将声明放在头 *.h 文件中。实现进入 *.cpp 文件。

地址.h

#pragma once
#include <string>

class Address
{
public:
    explicit Address();
    explicit Address(const std::string& city, const std::string& state,
        const std::string& street, const std::string& zip);
    const std::string& getCity() const;
    const std::string& getState() const;
    const std::string&  getStreet() const;
    const std::string&  getZip() const;
    void printAddress() const;
private:
    std::string street;
    std::string city;
    std::string state;
    std::string zip;
};

地址.cpp

#include "Address.h"
#include <iostream>

// Default Constructor
Address::Address() :
    city("Beverly Hills"),
    state("CA"),
    street("99999 Sunset Boulevard"),
    zip("99999")
{ }

Address::Address(const std::string& city, const std::string& state,
    const std::string& street, const std::string& zip) :
    city(city), state(state), street(street), zip(zip)
{ }

const std::string& Address::getCity() const
{
    return city;
}
const std::string& Address::getState() const
{
    return state;
}
const std::string& Address::getStreet() const
{
    return street;
}
const std::string& Address::getZip() const
{
    return zip;
}
void Address::printAddress() const
{
    // removed the endl here !
    std::cout << "Address: " << street << ", " << city << ", "
        << state << ", " << zip;
};

名称.h

#pragma once
#include <string>

class Name
{
public:
    explicit Name(const std::string& firstName, const std::string& lastName);
    void printName() const;
private:
    std::string firstName;
    std::string lastName;
};

名称.cpp

#include "Name.h"
#include <iostream>

Name::Name(const std::string& firstName, const std::string& lastName) :
    firstName(firstName), lastName(lastName)
{ }

void Name::printName() const
{
    std::cout << "Name: " << lastName << ", " << firstName;
}

员工.h

#pragma once
#include <string>
#include "Name.h"
#include "Address.h"

class Employee
{
public:
    explicit Employee(const Name& name, const Address& address, const std::string& ssn);
    void printEmployee() const;
private:
    Name name;
    Address address;
    std::string ssn;
};

员工.cpp

#include "Employee.h"
#include <iostream>

Employee::Employee(const Name& name, const Address& address, const std::string& ssn) :
    name(name), address(address), ssn(ssn)
{ }

void Employee::printEmployee() const
{
    std::cout << "Employee: ";
    name.printName();
    std::cout << ", ";
    address.printAddress();
    std::cout << ", ssn: " << ssn << std::endl;
}

主文件

#include <iostream>
#include <string>
#include "Employee.h"

int main(int argc, char* argv[]) {

    Address address("Cologne", "NRW", "Domplatz 1", "D-50668");
    Name name("John", "Jones");

    Employee employee(name, address, "123-abc-456");
    employee.printEmployee();

    return 0;
}

那很有趣!:-)

编辑:

我想我需要补充一点解释:

如果你想在另一个 cpp 文件中使用一个 cpp 文件中的函数或方法,你应该分开声明和实现。实际上,您应该始终这样做。将声明放在 *.h 文件中,并将实现放在 *.cpp 文件中。在要使用函数的 cpp 文件中包含声明函数的头文件。然后将所有 cpp 文件编译为目标文件。然后将所有对象文件链接在一起。

例如查看 Employee.cpp 文件。在那里,我使用了 Address 类的 print 方法。现在查看 Employee.cpp 文件的包含。在那里你看到我包含了Employee.h,而Employee.h 又包含了Address.h。包括实际上只是将一个文件的内容插入到另一个文件中。所以现在我们已经在 Employee cpp 文件中包含了 Address 类和方法的声明,以便可以从那里调用它。

另一个更新

我测试了我昨天发布的代码。它工作正常。问题开始是因为您想像这样更改 printEmployee() 方法:

void Employee::printEmployee() const
{
    cout << Name.printName() << Address.printAddress() << ssn << std::endl;
}

这仅在 printName() 和 printAddress() 方法不打印而是返回字符串时才有效。在这种情况下,软件开发人员通常会命名该方法ToString()或类似名称。但我们将保留原名,因为我们坚持您教授的任务。

您必须更改头文件中方法的声明以返回字符串。然后您还必须更改 cpp 文件中的实现以返回一个字符串。给你(不是完整的源代码,只有更改):

地址.h

class Address
{
public:
    // .....
    std::string& printAddress() const;
    // .....
};

地址.cpp

// Add this new include to the others at the top of the file:
#include <sstream>

// ......

std::string& Address::printAddress() const
{
    std::stringstream ss;
    ss << "Address: " << street << ", " << city << ", "
        << state << ", " << zip;
    return ss.str();
}

名称.h

class Name
{
public:
    // .....
    std::string& printName() const;
    // .....
};

名称.cpp

// Add this new include to the others at the top of the file:
#include <sstream>

// ......

std::string& Name::printName() const
{
    std::stringstream ss;
    ss << "Name: " << lastName << ", " << firstName;
    return ss.str();
}

现在你可以像你想要的那样在 Employee 类中使用它们:

员工.cpp

void Employee::printEmployee() const
{
    std::cout << Name.printName() << Address.printAddress() << ssn << std::endl;
}

最终编辑

在阅读了您发布的作业的详细信息后,我认为我的第一个解决方案很好。你的教授清楚地说printAddress()andprintName()方法应该打印不返回字符串。所以也许你应该考虑使用我第一个解决方案中的代码。


推荐阅读