【问题标题】:Struct argument inside a function definition that is in a different file位于不同文件中的函数定义中的结构参数
【发布时间】:2014-02-05 17:43:39
【问题描述】:

我有一个任务,我必须为我的函数使用不同的文件,并且我不必使用头文件。问题是编译器向我显示了各种错误。我的所有函数的所有错误都完全相同。

这是错误:

1>----- 构建开始:项目:Asignacion 1,配置:调试 Win32 ------

1> 排序公司.cpp

1>c:\users\emanuel\documents\visual studio 2013\projects\asignacion 1\asignacion 1\sortcompany.cpp(1): error C2065: 'Elemento' : undeclared identifier

1>c:\users\emanuel\documents\visual studio 2013\projects\asignacion 1\asignacion 1\sortcompany.cpp(1):错误 C2146:语法错误:在标识符“经销商”之前缺少 ')'

1>c:\users\emanuel\documents\visual studio 2013\projects\asignacion 1\asignacion 1\sortcompany.cpp(1):错误 C2182:“SortCompany”:非法使用“void”类型

1>c:\users\emanuel\documents\visual studio 2013\projects\asignacion 1\asignacion 1\sortcompany.cpp(1):错误 C2059:语法错误:')'

1>c:\users\emanuel\documents\visual studio 2013\projects\asignacion 1\asignacion 1\sortcompany.cpp(2):错误 C2143:语法错误:缺少 ';'在'{'之前

1>c:\users\emanuel\documents\visual studio 2013\projects\asignacion 1\asignacion 1\sortcompany.cpp(2): 错误 C2447: '{' : 缺少函数头(旧式正式列表? )

(所有文件的错误与上述相同.....)

1> 生成代码... ========== 构建:0 成功,1 失败,0 最新,0 跳过 ==========


我有 main.cpp,它是主文件(显然)。主调用“Menu.cpp”,菜单调用其他文件中的所有其他函数。

PD:Elemento[] 是一个结构

Menu.cpp 代码:

using namespace std;

//these functions are fine
void Closing(ifstream &, ofstream &);
void Opening(ifstream &, ofstream &);

//the problem is here with all these functions
void Registro(Elemento[], int &, ifstream &);
void InfoDealer(Elemento[], int, ofstream &);
void SortCompany(Elemento[], int);
void MayorVentas(Elemento[], int, ofstream &);
void MayorVentasPorMarca(Elemento[], int, ofstream &);



#include "Registro.cpp"
#include "InfoDealer.cpp"
#include "SortCompany.cpp"
#include "MayorVentas.cpp"
#include "MayorVentasPorMarca.cpp"

void Menu()
{
//code here...
}

Registro.cpp:

void Registro(Elemento Dealer[], int &Cantidad, ifstream &entrada)
{
//code here...
}

ma​​in.cpp:(这个文件是默认的。我不应该改变它。)

//include stuff
using namespace std;

const int MAXIMODEALERS = 20;
const int MAXIMOMODELOS = 6;
struct Detail
{
    string ModelName;
    int Sales;
};
typedef Detail Detalle;

struct Element
{
    string   CompanyName;
    Detalle  Modelo[MAXIMOMODELOS];
};
typedef Element Elemento;

Elemento Dealer[MAXIMODEALERS];

int Cantidad;

void Menu(void);


#include "Menu.cpp"

void Opening(ifstream &Entrada, ofstream &Salida)
{
//code
}

void Closing(ifstream &entrada, ofstream &salida)
{
//code
}

int main()
{
    Menu();
    return 0;
}

所有其余文件的结构都与 Registro.cpp 相同

希望你能帮助我!如果您需要更多详细信息,请询问。

【问题讨论】:

  • 您确定这是一门C++课程,而不是一门C课程吗?
  • 有什么建议吗?粉碎...?
  • 你能告诉我们你的类的声明吗?
  • @0x499602D2 我添加了 main.cpp 但我们仍然没有使用类。下一个任务是将此程序更改为类:P
  • 您不应该包含.cpp 文件。您应该改为包含头文件。将类及其方法的声明放入.h 文件中,并使用.cpp 文件来实现这些功能。在main.cpp 中,只需执行#include "Registro.h"#include "Menu.h"...

标签: c++ file struct visual-studio-2013


【解决方案1】:

您应该在使用类和结构之前对其进行前向声明。在函数前添加struct Elemento;。而且无论如何你不能在不包含ifstream的情况下使用ifstream

【讨论】:

  • 是的,我在 main.cpp 中包含了所有库。谢谢!
【解决方案2】:

翻译单元中使用的每个名称都必须首先声明,在使用它的语句之上。例如,您的函数Registro 使用名称Elemento。编译器对此一无所知,因此您必须首先在某处声明它以指定其类型。这同样适用于ifstreamofstream。另外,我认为您正在尝试链接 Menu.cpp 以及其他文件中函数的所有定义。那么你就有麻烦了,因为每个函数都有多个定义,一个定义是 include 它在 Menu.cpp 中,另一个定义在每个源文件中。当您链接所有这些时,您会为每个函数获得两个定义。另外,请注意这是一个糟糕的设计。你永远不应该 include .cpp 文件。那是NOT,因为你不被允许。预处理器不关心你是 include .cpp 还是 .abcdef 文件。它们都只是文本文件。有了这个,我的意思是你不应该在你的翻译单元中包含definitions,因为它们会给你带来严重的头痛,这是你不能指望的非常糟糕的设计。这是一个很好的解释如何组织你的代码。希望这会有所帮助!

补充一点:

我在下面看到你的评论。是的,Element 是 main.cpp 中的 defined(未声明)。但是,因为您正在单独编译所有这些文件,所以编译器不知道在您指定的翻译单元 unless 之外存在什么。因此,如果您在使用之前不知道 declare 它,它就不会知道名称 Element。我认为您对一些概念了解甚少,例如:翻译单元、预处理 include-s、链接、范围、存储持续时间等。花点时间阅读它,让一切变得更加清晰。

How to successfuly organize your source code in files.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-29
    • 2020-10-18
    • 2021-06-09
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    相关资源
    最近更新 更多