简体   繁体   English

另一个标头中的C ++ Struct函数提供“缺少类型说明符”

[英]C++ Struct function in an another header gives “missing type specifier”

I got in my "main.cpp" the following code: 我在“ main.cpp”中输入以下代码:

#include "rational.h"

using namespace std;

typedef struct rational {
    long long numerator;
    long long denominator;
} rational_t;

And I have a Header-file namend "rational.h": 我有一个头文件名为“ rational.h”:

#pragma once


rational add(rational  a,rational b)
{
    rational c;
    c.numerator = a.numerator + b.numerator;
    c.denominator = a.denominator + b.denominator;
    return c;
}

I got an Error on Line: 我在网上遇到了一个错误:

rational add(rational  a,rational b)

It gives me the following Error-Code: Fehler C4430 Fehlender Typspezifizierer - int wird angenommen. 它给了我下面的错误代码:Fehler C4430 Fehlender Typspezifizierer-int wird angenommen。 Hinweis: "default-int" wird von C++ nicht unterstützt. Hinweis:“ default-int”与C ++兼容。 Translation: Error C4430 Missing Type specifier - int is accepted. 翻译:错误C4430缺少类型说明符-接受int。 "default-int" is not supported by C++. C ++不支持“ default-int”。

I think because the function does not detect my struct properly. 我认为是因为该函数无法正确检测到我的结构。 Can anyone pls help me? 有人可以帮助我吗?

Greetings, Nike 问候,耐克

You haven't pasted your full source code for main.cpp and rational.h, so this makes it a little harder to debug properly. 您尚未为main.cpp和Rational.h粘贴完整的源代码,因此这使得正确调试变得有些困难。 Based only on what you've given: 仅基于您提供的内容:

  1. Your structure was called "rational" but you've used typedef and defined it as a new type, or another way of declaring a "struct rational" called rational_t. 您的结构被称为“合理的”,但是您已经使用typedef并将其定义为新类型,或者是另一种声明“结构有理”的方式称为Rational_t。 Your function should return a rational_t, and accept rational_t for both parameters. 您的函数应该返回一个Rational_t,并接受两个参数的Rational_t。

  2. You likely meant to put the struct rational into rational.h, ahead of your function declaration. 您可能打算在函数声明之前将结构有理放入Rational.h中。

It's pretty hard to determine if you want to use C++ or C from this example code too, so I've written it up in C. It'll be a start for you to learn from. 从这个示例代码中很难确定您是否还要使用C ++或C,因此我已经用C编写了它。这将是您学习的起点。

main.c main.c

#include <stdlib.h>
#include <stdio.h>

#include "rational.h"

int main() 
{
    rational_t first;
    rational_t second;

    first.numerator = 5;
    first.denominator = 7;

    second.numerator = 3;
    second.denominator = 9;

    rational_t product = add(first, second);
    printf("%lld / %lld\n", product.numerator, product.denominator);

    return 0;
}

rational.h 有理数

#ifndef RATIONAL_H_
#define RATIONAL_H_

typedef struct rational {
    long long numerator;
    long long denominator;
} rational_t;

rational add(rational  a,rational b)
{
    rational c;
    c.numerator = a.numerator + b.numerator;
    c.denominator = a.denominator + b.denominator;
    return c;
}

#endif // RATIONAL_H_

Your rational.h header can't see the definition of rational but is trying to use it. 您的rational.h标头看不到rational的定义,但正在尝试使用它。

Move typedef struct rational… to the top of rational.h and replace with a declaration in main.cpp : 移动typedef struct rational…到顶部rational.h ,并在声明中替换main.cpp

#include "rational.h"

using namespace std;

rational rational_t;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM