【问题标题】:How to assign values to a struct type variable? [duplicate]如何为结构类型变量赋值? [复制]
【发布时间】:2020-03-08 16:59:14
【问题描述】:

所以我的编译器抱怨我的 destination.travelTime 有 3 个参数,虽然它应该有 3 个参数,关于如何解决这个问题的任何建议还是我错了? TIA

#include <iostream>

using namespace std;


struct timeType
{
    int hr;
    double min;
    int sec;
};

struct tourType
{
    string cityName;
    int distance;
    timeType travelTime;
};


int main()
{
    tourType destination;

    destination.cityName = "Nottingham";
    destination.distance = 130;
    destination.travelTime (3, 15.0, 0);
    return 0;
}

【问题讨论】:

标签: c++ visual-c++ struct


【解决方案1】:

destination.travelTime (3, 15.0, 0); 是函数调用,而不是赋值。您需要这样做:

destination.travelTime = timeType{3, 15.0, 0};

或者

destination.travelTime = {3, 15.0, 0};

取决于您的 C++ 版本。

【讨论】:

  • 是的,这个问题已经被问过了,谢谢你们的时间:)
猜你喜欢
  • 1970-01-01
  • 2015-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多