首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >E0153 -表达式必须具有类类型,但它具有"void“类型。

E0153 -表达式必须具有类类型,但它具有"void“类型。
EN

Stack Overflow用户
提问于 2022-02-12 06:37:12
回答 2查看 670关注 0票数 -4

我一直在努力使我的计划发挥作用,但我遇到了一个问题。

我在Visual上遇到的错误是:表达式必须有类类型,但是它有类型"void"

错误来自我在第24行中的main.cpp文件:

代码语言:javascript
运行
复制
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "Robot.h"

using namespace std;
using namespace sdds;

int main()
{
    const int num_robots = 6;
    int broken_robot = 0;
    char replacmentName[] = "C3PO";
    Robot robot[num_robots] = {
        {},
        {"KC1", "kitchen", 25.33, 4.55, 2, 2.2, false},
        {"BR1", "bedroom", 5.22, 2.54, 1, 2.2, true},
        {"Broken", "Bedroom", 10.12, 2.5,1.55, 0, true},
        {"KC2", "kitchen", 20.56, 5, 2, 3.5, true},
        {"BR2", "bedroom", 25.32, 6.5, 2.5, 3.1, true}
    };

    while ((broken_robot = conrtolRooomReport(robot, num_robots)) >= 0) {
        cout << endl << "Attention: There is a broken robot! Fixing the problem..." << endl;
        robot[broken_robot].set(replacmentName, "Control Room", 10.0, 4, 1, 2.09, false).display();
        replacmentName[0]++;
        cout << "Replacement Provided!" << endl << endl;
    }

    return 0;
}

Robot.h头文件:

代码语言:javascript
运行
复制
#ifndef _ROBOT_H
#define _ROBOT_H
namespace sdds {
    class Robot {
        char* name;
        char* location;
        double weight;
        double width;
        double height;
        double speeed;
        bool deployed;
    public:
            // Constructs & Destructor:
        Robot();
        Robot(const char* newName, const char* newLoc, double newWeight, double newWidth, double newHeight, double newSpeed, bool deploy);
        ~Robot();
            // Queries:
        char* getName() const;
        char* getLocation() const;
        bool isDeployed() const;
        bool isValid() const;
        double speed() const;
        void display() const;
            // Modifiers
        void set(const char* newName, const char* newLoc, double newWeight, double newWidth, double newHeight, double newSpeed, bool deploy);
        void setLocation(char* newestLoc);
        void setDeployed(bool deploy);
    };
            // Other Functions:
    int conrtolRooomReport(const Robot robot[], int num_robots);
}
#endif

显示()函数实现:

代码语言:javascript
运行
复制
void Robot::display() const{
    cout << "| ";
    cout.setf(ios::left);
    cout.width(10);
    cout << name << " | ";
    cout.width(15);
    cout << location << " | ";
    cout.setf(ios::right);
    cout.setf(ios::fixed);
    cout.width(6);
    cout << setprecision(1) << weight << " | ";
    cout.width(6);
    cout << setprecision(1) << width << " | ";
    cout.width(6);
    cout << setprecision(1) << height << " | ";
    cout.width(6);
    cout << setprecision(1) << speeed << " | ";
    cout.width(8);
    cout.unsetf(ios::right);
    cout.setf(ios::left);
    if(isDeployed() == true)
        cout << "YES" << " |" << endl;
    else
        cout << "NO" << " |" << endl;
    cout.unsetf(ios::left);
}

set()函数实现:

代码语言:javascript
运行
复制
void Robot::set(const char* newName, const char* newLoc, double newWeight, double newWidth, double newHeight, double newSpeed, bool deploy) {
    if (newName[0] != '\0' && newName != nullptr && newLoc[0] != '\0' && newLoc != nullptr && newWeight > 0.00 && newWidth > 0.00 && newHeight > 0.00 && newSpeed > 0.00) {
        name = new char[strlen(newName) + 1];
        strcpy(name, newName);
        location = new char[strlen(newLoc) + 1];
        strcpy(location, newLoc);
        weight = newWeight;
        width = newWidth;
        height = newHeight;
        speeed = newSpeed;
        deployed = deploy;
    }
}

任何帮助都将不胜感激。试着搜索谷歌,但没有找到解决方案。

EN

回答 2

Stack Overflow用户

发布于 2022-02-12 06:51:19

替换

代码语言:javascript
运行
复制
robot[broken_robot].set(replacmentName, "Control Room", 10.0, 4, 1, 2.09, false).display();

使用

代码语言:javascript
运行
复制
robot[broken_robot].set(replacmentName, "Control Room", 10.0, 4, 1, 2.09, false);
robot[broken_robot].display();
票数 1
EN

Stack Overflow用户

发布于 2022-02-12 07:26:48

因此,我不得不将set();函数的返回类型更改为Robot&一切都正常工作。感谢每一个贡献和帮助的人!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71089557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档