枚举麻烦:重新声明为不同类型的符号

发布于 2024-10-16 04:59:44 字数 1488 浏览 5 评论 0原文

我正在编写一个程序,旨在帮助我了解 C++ 中的枚举数据类型。当前的问题是,当我尝试使用新数据类型时,编译器不喜欢我使用枚举,就像我使用其他数据类型一样。编译 tangleShape 函数时出现错误“重新声明为不同类型的符号”。看一下相关代码。任何见解表示赞赏!谢谢!

(所有函数都是自己的.cpp文件。)

头文件

#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED

#include <iostream>
#include <iomanip>

using namespace std;

enum triangleType {noTriangle, scalene, isoceles, equilateral};

//prototypes
void extern input(float&, float&, float&);
triangleType extern triangleShape(float, float, float);
/*void extern output (float, float, float);*/
void extern myLabel(const char *, const char *);



#endif // HEADER_H_INCLUDED

主函数

//8.1 main
// this progam...

#include "header.h"

int main()
{
    float sideLength1, sideLength2, sideLength3;
    char response;


     do //main loop
      {
           input (sideLength1, sideLength2, sideLength3);
           triangleShape (sideLength1, sideLength2, sideLength3);
           //output (sideLength1, sideLength2, sideLength3);
           cout << "\nAny more triangles to analyze? (y,n) ";
           cin >> response;
      }
    while (response == 'Y' || response == 'y');

    myLabel ("8.1", "2/11/2011");

    return 0;
}

triangleShape形状

# include "header.h"

triangleType triangleShape(sideLenght1, sideLength2, sideLength3)
{
    triangleType triangle;
    return triangle;
}

I am writing a program that is supposed to help me learn about enumeration data types in C++. The current trouble is that the compiler doesn't like my enum usage when trying to use the new data type as I would other data types. I am getting the error "redeclared as different kind of symbol" when compiling my trangleShape function. Take a look at the relevant code. Any insight is appreciated! Thanks!

(All functions are their own .cpp files.)

header file

#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED

#include <iostream>
#include <iomanip>

using namespace std;

enum triangleType {noTriangle, scalene, isoceles, equilateral};

//prototypes
void extern input(float&, float&, float&);
triangleType extern triangleShape(float, float, float);
/*void extern output (float, float, float);*/
void extern myLabel(const char *, const char *);



#endif // HEADER_H_INCLUDED

main function

//8.1 main
// this progam...

#include "header.h"

int main()
{
    float sideLength1, sideLength2, sideLength3;
    char response;


     do //main loop
      {
           input (sideLength1, sideLength2, sideLength3);
           triangleShape (sideLength1, sideLength2, sideLength3);
           //output (sideLength1, sideLength2, sideLength3);
           cout << "\nAny more triangles to analyze? (y,n) ";
           cin >> response;
      }
    while (response == 'Y' || response == 'y');

    myLabel ("8.1", "2/11/2011");

    return 0;
}

triangleShape shape

# include "header.h"

triangleType triangleShape(sideLenght1, sideLength2, sideLength3)
{
    triangleType triangle;
    return triangle;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌逼全场 2024-10-23 04:59:44

你的问题与枚举无关。问题是你的 triangleShape 定义中的这一行:

triangleType triangleShape(sideLenght1, sideLength2, sideLength3)

你缺少参数的类型,并且某些编译器(例如 gcc)默认为 int (尽管这不是标准行为,所以你永远不应该依赖它)。由于函数定义使用float,编译器将其视为以不同方式重新声明。您应该在实现中使用指定的float

triangleType triangleShape(float sideLenght1, float sideLength2, float sideLength3)

Your problem has nothing to do with enums. The problem line is this line in your triangleShape definition:

triangleType triangleShape(sideLenght1, sideLength2, sideLength3)

You're missing the types for your parameters and some compilers such as gcc default to int (although this isn't standard behaviour so you should never rely on it). Since the function definition uses floats the compiler sees it as redeclaring it differently. You should use specify floats in the implementation:

triangleType triangleShape(float sideLenght1, float sideLength2, float sideLength3)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文