簡體   English   中英

C++ 中的編譯錯誤

[英]compilation error in C++

我在 C++ 中有以下代碼,我得到了編譯錯誤:

a.cpp: In member function `virtual void Derived<T, D>::run(T&)':
a.cpp:13: error: expected primary-expression before "int"
a.cpp:13: error: expected `;' before "int"

請幫我找出這里出了什么問題。 非常感謝。

#include <iostream>
template<typename T> struct Base
{
    virtual void run( T& ){}
    virtual ~Base(){}
};

template<typename T, typename D> struct Derived : public Base<T>
{
    virtual void run( T& t )
    {
        D d;
        d.operator()<int>();//nor does d.operator()<T>(); work
    }
};

template<typename T> struct X
{
    template<typename R>  X(const R& r)
    {
       std::cout << "X(R)" << std::endl;
       ptr = new Derived<T,R>(); 
    }

    X():ptr(0)
    { 
        std::cout << "X()" << std::endl; 
    }

    ~X()
    {
        if(ptr) 
        {
            ptr->run(data);
            delete ptr;
        }
        else
        {
            std::cout << "no ptr" << std::endl;
        }
    }

    Base<T>* ptr; 
    T data;
};

struct writer
{
    template<typename T> void operator()()
    { 
        std::cout << "T "<< std::endl;
    }
};

int main()
{
    {
        writer w;
        X<int> xi1((writer()));
    }
    return 0;
};

Derived<>::run()中,更改

d.operator()<int>();

d.template operator()<int>();

有關詳細信息,請參閱此常見問題解答:
->template.template::template語法是關於什么的?

您的原始版本在使用 Visual Studio 2008 附帶的 Microsoft C++ 編譯器版本 15.00.21022.08 編譯時有效,並帶有以下消息:

C:\Program Files\Microsoft Visual Studio 9.0\VC\INCLUDE\xlocale(342) : 
warning C 4530: C++ exception handler used, but unwind semantics are not enabled. 
Specify /EHsc 
Microsoft (R) Incremental Linker Version 9.00.21022.08 Copyright (C) Microsoft Corporation.  All rights reserved.

/out:a.exe a.obj

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM