【问题标题】:Error: expected a declaration ......... and '{' : missing function header (old-style formal list?)错误:期望声明............和'{':缺少函数头(旧式正式列表?)
【发布时间】:2021-07-11 08:04:07
【问题描述】:

我尝试使用此代码创建一个 DLL,但在第 7 行,我收到这两个错误“E0169 - 需要声明”和“C2447 - '{':缺少函数头(旧式正式列表?)”

【问题讨论】:

  • 错字? class GDT_dll; { -> class GDT_dll {? public void main(); -> public: void main();?哦,你的couts 和cins 不在函数范围内,因为它们必须在。附加建议:向good C++ book学习C++,而不是随机编码。
  • 您需要学习 C++ 语法的基础知识。 (现在开始考虑创建 DLL 还为时过早。您需要先了解基础知识,甚至更多。)

标签: c++ dll compiler-errors visual-studio-2017 syntax-error


【解决方案1】:

你留下了一个;在你的班级名称之后。

您还应该在函数定义的 public 之后加上 :。

另外,如果你尝试在你的类中定义一个函数,语法应该如下:

class className
{
    int attribute;

    public : void foo(){
        cout << "Value of attribute is " << attribute << endl;
    }
};

也就是说没有 ;并在括号后加上 {。

我建议你看看this link 以了解如何正确创建一个类。

【讨论】:

    【解决方案2】:

    1、创建类时,GDT_dll后面不能有;

    class GDT_dll
    {
    ……
    }
    

    2,你需要:after public

    3、不能将函数命名为“main”,因为main function有特殊含义。

    4、最后,在最后一个“}”之后需要一个“;”

    代码如下:

    #include"pch.h"
    #include <stdio.h>
    #include <iostream>
    #include <string>
    #include <conio.h>
    using namespace std;
    class GDT_dll
    {
        public : void foo()
        {
        string name, firstName;
        int birthDate, birthMonth, birthYear;
    
            cout << "Enter Name: ";
            cin >> name;
            cout << "Enter First Name: ";
            cin >> firstName;
            cout << "Enter Birth Date: ";
            cin >> birthDate;
            cout << "Enter Birth Month: ";
            cin >> birthMonth;
            cout << "Enter Birth Year: ";
            cin >> birthYear;
        }
    };
    

    如果你想创建一个DLL,我建议你可以参考文档:Walkthrough:Create and use your own Dynamic Link Library

    【讨论】:

    • @A.MUSTAQ AHMED 你有任何更新吗?如果您的案例已经解决,请帮忙标记答案。如果没有,请随时与我们联系。您的理解与合作将不胜感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多