weixin_46417205
weixin_46417205
2021-01-21 08:24

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

50
  • c++

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条回答