【问题标题】:error: C2059: syntax error : '{'错误:C2059:语法错误:'{'
【发布时间】:2014-11-25 09:06:39
【问题描述】:

我想在 qt creator 中运行一个应用程序。但是当我点击构建时,它在 carddetect.cpp 中显示错误

此处出现错误:void CardDetect::aamvaIssuerList()

但我不知道那个错误是什么。

124: error: C2059: syntax error : '{'
124: error: C2143: syntax error : missing ';' before '{'
124: error: C2143: syntax error : missing ';' before '}'

这是我的代码:

#include "carddetect.h"
#include <QDebug>

void CardDetect::aamvaIssuerList(){
        issuerList [ "636026" ] =  (struct issuer) {"Arizona", "AZ", "L"};
        issuerList [ "0636021"] =  (struct issuer) { "Arkansas", "AR", "" };
        issuerList [ "636014" ] =  (struct issuer) { "California", "CA", "L" };
        issuerList [ "636020" ] =  (struct issuer) { "Colorado", "CO", "NN-NNN-NNNN" };
        issuerList [ "636010" ] =  (struct issuer) { "Florida", "FL", "LNNN-NNN-NN-NNN-N" };
        issuerList [ "636018" ] =  (struct issuer) { "Iowa", "IA", "NNNLLNNNN" };
    }

carddetect.h 是

#ifndef CARDDETECT_H
#define CARDDETECT_H

#include <QMap>
#include "magcard.h"

struct issuer {
    QString name;
    QString abbreviation;
    QString format;
};

class CardDetect {
    public:
        CardDetect( MagCard *_card = 0 );
        void setCard( MagCard *_card ); 

    private:
        MagCard *card;

        void processCard();

        void luhnCheck();
        void creditCardCheck();
        void aamvaCardCheck( QString expDate );
        void aamvaIssuerList();
        QMap<QString,struct issuer> issuerList;

};


#endif // CARDDETECT_H

该代码工作@keltar

但现在在这个函数中

void CardDetect::aamvaCardCheck( QString expDate ) {
    if( card->encoding == IATA )
        return; //we're only going to support ABA for now
    struct issuer issuerInfo;

QString iin = card->accountNumber.left( 6 );

issuerInfo = issuerList.value( iin );
if( issuerInfo.name.isEmpty() ) {
    iin = card->accountNumber.mid( 1, 6 );
    issuerInfo = issuerList.value( iin );
    if( issuerInfo.name.isEmpty() )
        return; // this is not a known AAMVA card, abort
}

显示错误

error: C2512: 'issuer' : no appropriate default constructor available
error: C2512: 'issuer::issuer' : no appropriate default constructor available

in struct issuer issuerInfo;

【问题讨论】:

  • both C 和 C++ 标记这个问题是没有意义的,因为它在 C 和 C++ 中意味着不同的东西。在 C(99) 中,这是有效代码,而在 C++ 中,则不是。那么,您使用的是哪种语言?
  • @TheParamagneticCroissant 你似乎很困惑。这显然是 C++ 而不是 C。
  • 您使用的构造 ((type){initialiser}) 是复合文字;它是 C11 或任何 GNU C/C++,但据我所知,它不是标准 C++,MSVC 不支持它。重写你的代码。
  • 好的,谢谢。我是 C++ 的初学者。请你帮我改一下@keltar
  • @DavidHeffernan 不,我并不感到困惑 - 只需查看问题的修订历史即可。当我的评论只包含第一个代码 sn-p (具有 C99 复合文字的代码)时,我已经写了我的评论。问题中还没有有效的 C++。现在很明显 OP 意味着它是 C++,但不幸的是它仍然不是。

标签: c++ syntax-error


【解决方案1】:

由于你不能使用复合文字,这里有一个例子来说明如何做到这一点:

将构造函数添加到您的结构中:

struct issuer {
    issuer(const char *nm, const char *abbr, const char *fmt) : name(nm),
         abbreviation(abbr), format(fmt) {}

    QString name;
    QString abbreviation;
    QString format;
};

并将您的功能更改为:

void CardDetect::aamvaIssuerList(){
    issuerList [ "636026" ] =  issuer("Arizona", "AZ", "L");
    // same for the rest of the lines
}

当然,还有其他方法可以做到这一点。

【讨论】:

    猜你喜欢
    • 2017-09-02
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-07
    相关资源
    最近更新 更多