gpt4 book ai didi

c++ - 从基类访问派生类的非成员函数

转载 作者:太空宇宙 更新时间:2023-11-04 14:58:29 25 4
gpt4 key购买 nike

我试图从基类调用派生类的非成员函数,但出现此错误:

error: no matching function for call to '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();
}

我的猜测是我缺少一个小语法。有什么想法吗?

最佳答案

您取消引用指针,因此您传递的是 const char,而不是 const char*。

试试这个:

generate_vectorlist(outfile_name);

关于c++ - 从基类访问派生类的非成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3089744/

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