【问题标题】:error: variable has incomplete type 'introstructions' can't understand错误:变量的类型不完整“介绍”无法理解
【发布时间】:2019-04-22 20:06:46
【问题描述】:
#include <iostream> 

using namespace std;

int main()
{
struct calcvaribles;
{
int a;
int b;
int sum;
};
struct introstructions;
{
char ab[35];
string ac;
};
introstructions outline;
outline.ab = "welcome to my calculator program!";
outline.ac = "please enter any [One] number in the 
terminal";
return 0;
}

错误信息:

聚合 main()::introstructions 大纲类型不完整,无法定义

介绍大纲。

【问题讨论】:

  • 我认为你的意思是 C++ 的标签,而不是 CSS
  • 你在'struct introstructions;'之后尾随分号错了
  • 好吧,我想我应该更加注意自己的笔记,哈哈。

标签: c++


【解决方案1】:
  • 为什么在main 中声明structs?只需将它们移入 全局范围。
  • 您不能像在此处那样将字符串文字分配给char 数组:outline.ab = "welcome to my calculator program!";。相反,使用const char* 或者更好的是,正如您在char 数组声明下方显示的那样,std::string,但不要忘记#include &lt;string&gt;

您的代码中的问题是您在 struct 名称之后包含了 ;

    struct calcvaribles;
    struct introstructions;

以下应该有效:

#include <iostream>
#include <string>

using namespace std;

int main() {
    struct calcvaribles {
        int a;
        int b;
        int sum;
    };

    struct introstructions {
        string ab;
        string ac;
    };

    introstructions outline;

    outline.ab = "welcome to my calculator program!";
    outline.ac = "please enter any [One] number in the terminal";

    return 0;
}

【讨论】:

  • 谢谢老兄,不胜感激。
【解决方案2】:

在这种情况下,C++ 语言无法帮助您,因为编译器以与您预期完全不同的方式解释代码。

#include <iostream> 

using namespace std;

int main()
{
struct calcvaribles;

由于上一行末尾的分号,编译器将其读取为:

有一个名为calcvaribles 的结构。目前尚不清楚该结构具有哪些字段,这些字段将在其他地方定义。在任何情况下,结构的名称都是已知的,如果您声明一个类型为指向该结构的指针的变量,那么从一开始就允许这样做。

{

这个左大括号的意思是:

一个新的范围从这里开始。在此范围内声明的任何变量都只能在对应的} 之前可用。

int a;
int b;
int sum;

这些不是结构字段(您想要的),而是普通变量的声明。这些变量什么都不做,它们没有赋值,它们的值根本不被读取。总之,这三行是一个“什么都不做”的大块。

};

范围到此结束。从现在开始,变量absum 不再存在。

分号也是没用的。这是一句空话。允许但没用。

struct introstructions;
{
char ab[35];
string ac;
};
introstructions outline;
outline.ab = "welcome to my calculator program!";
outline.ac = "please enter any [One] number in the 
terminal";
return 0;
}

introstructions 类型也是如此。

如果您在 main 函数之外编写了 struct 定义,编译器会给您一个“语法错误”消息。也许您先这样做了,为了修复错误消息,您将所有内容都移到了 main 函数中。

不幸的是,仅仅因为这些尾随分号,您编写的代码仍然可以编译,但意味着完全不同的东西。但是没有解决方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多