从基类访问派生类的非成员函数
我试图从基类调用派生类的非成员函数,但出现此错误:
错误:没有匹配的函数可用于调用“generate_vectorlist(const char&)”
这是来自基类的相关代码片段:
//Element.cpp
#include "Vector.h"
...
string outfile;
cin >> outfile;
const char* outfile_name = outfile.c_str();
generate_vectorlist(*outfile_name); //ERROR
...
和派生类(这是一个模板类,所以所有内容都在标头中):
//Vector.h
template <class T>
void generate_vectorlist(const char* outfile_name = "input.txt" )
{
std::ofstream vectorlist(outfile_name);
if (vectorlist.is_open())
for (Element::vciter iter = Element::vectors.begin(); iter!=Element::vectors.end(); iter++)
{
Vector<T>* a = new Vector<T>(*iter);
vectorlist << a->getx() << '\t' << a->gety() << '\t'<< a->getz() << std::endl;
delete a;
}
else { std::cout << outfile_name << " cannot be opened." << std::endl;}
vectorlist.close();
}
我的猜测是只有一个我缺少的小语法东西。有什么想法吗?
I'm trying to call a nonmember function of a derived class from a base class, but getting this error:
error: no matching function for call to 'generate_vectorlist(const char&)'
Here's the relevant code snippets from the base class:
//Element.cpp
#include "Vector.h"
...
string outfile;
cin >> outfile;
const char* outfile_name = outfile.c_str();
generate_vectorlist(*outfile_name); //ERROR
...
and the derived class (this is a template class, so everything's in the header):
//Vector.h
template <class T>
void generate_vectorlist(const char* outfile_name = "input.txt" )
{
std::ofstream vectorlist(outfile_name);
if (vectorlist.is_open())
for (Element::vciter iter = Element::vectors.begin(); iter!=Element::vectors.end(); iter++)
{
Vector<T>* a = new Vector<T>(*iter);
vectorlist << a->getx() << '\t' << a->gety() << '\t'<< a->getz() << std::endl;
delete a;
}
else { std::cout << outfile_name << " cannot be opened." << std::endl;}
vectorlist.close();
}
My guess is there's just a small syntax thing that I'm missing. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您取消引用指针,因此传递的是 const char,而不是 const char*。
试试这个:
You're dereferencing the pointer so you're passing a const char, not a const char*.
try this:
您有两个问题:
generate_vectorlist
采用const char *
,而不是const char &
。模板类型不在函数签名中,因此编译器无法推断出类型,因此您需要指定它(在我的示例中使用
int
)。所以你需要做:
You have two problems:
generate_vectorlist
takes aconst char *
, not aconst char &
.The template type is not in the function signature so the compiler can not deduce the type therefore you need to specify it (using
int
in my example).So you need to do:
首先,尝试:
生成向量列表(输出文件名);
您应该传递一个字符指针,而不是一个字符。
In the first bit, try:
generate_vectorlist(outfile_name);
You should be passing a character pointer, not a character.
您需要指定模板参数。编译器无法使用任何东西来推断
T
是什么类型。因此,您必须像generate_vectorlist(outfile_name);
那样调用它,并使用MyType
的适当类型。You need to specify the template argument. There's nothing the compiler can use to deduce what type
T
is. So you'll have to call it likegenerate_vectorlist<MyType>(outfile_name);
, using the appropriate type forMyType
.这就是问题所在:
编译器无法推断出 T 的类型,因此它不知道要使用哪个
generate_vectorlist
。像这样称呼它:
尽管我实际上建议这段代码首先没有任何意义。
This is the problem:
The compiler can't deduce the type of T, so it has no idea which
generate_vectorlist
to use.Call it like this:
Though I would actually suggest that this code doesn't make any sense in the first place.