gpt4 book ai didi

c++ - C++中的编译错误

转载 作者:搜寻专家 更新时间:2023-10-31 00:45:17 25 4
gpt4 key购买 nike

我在 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>();

有关更多信息,请参阅此常见问题解答:
What is the ->template , .template and ::template syntax about?

关于c++ - C++中的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7072437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com