weixin_46417205 2021-01-21 08:24 采纳率: 0%
浏览 55
已结题

这块为什么报错?挺急的,帮忙看一下,谢谢!

main.cpp中的int main()一行报错,报错信息是[Error] a function-definition is not allowed here before '{' token

**main.cpp关键代码如下:**:

#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <list>
#include <vector>
#include "include.h"

using namespace std;

int main () {
    //((FileCatalogLoader) catalogLoader).loadCatalog("catalog.txt");
            
    cout << "Welcome to the vending machine" << endl;
    
    //run();
        
    //writeFile("catalog.txt",catalogFormatter.formatCatalog(catalog));
    
    return 0;
}
```

**include.h代码如下:**

#include "Catalog.h"
#include "CatalogLoader.h"
#include "DataCatalogFormatter.h"
#include "DataSalesFormatter.h"
#include "Drink.h"
#include "FileCatalogLoader.h"
#include "Food.h"
#include "OperatorCatalogLoader.h"
#include "Order.h"
#include "OrderItem.h"
#include "Password.h"
#include "Product.h"
#include "Sales.h"
#include "Vault.h"

extern Catalog* catalog;
catalog = Catalog::getSingletonInstance();
extern Sales*  sales;
sales = Sales::getSingletonInstance();
extern Password* pw;
pw = Password::getSingletonInstance();
extern Vault* vault;
vault = Vault::getSingletonInstance();
extern CatalogLoader* catalogLoader;
catalogLoader = FileCatalogLoader::getSingletonInstance();
extern DataSalesFormatter* salesFormatter;
salesFormatter = DataSalesFormatter::getSingletonInstance();
extern DataCatalogFormatter* catalogFormatter;
catalogFormatter = DataCatalogFormatter::getSingletonInstance();

报错情况:
main.cpp中的int main()一行报错,报错信息是[Error] a function-definition is not allowed here before '{' token

对于这个错误,网上查的都是函数中定义函数、缺括号或分号导致的,可是我的代码不存在以上的问题,那是为什么出错呢?

另外,我把头文件#include "include.h"去掉,这块就不报错了,这是为什么呢?

希望有人能帮我解答一下这个问题,非常感谢!

  • 写回答

8条回答 默认 最新

  • SoftwareTeacher 《编程之美》作者 2021-01-21 08:32
    关注

    建议:

    从最简单的 main() 函数开始写起,把 include.h 也从最简单写起, 每一次都编译。 个人认为是你的 include.h 有一些不标准的写法,导致 main() 那里报错。 

    评论

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流