gpt4 book ai didi

c++ - 从派生类函数调用基函数?

转载 作者:行者123 更新时间:2023-11-28 06:43:49 27 4
gpt4 key购买 nike

类 BaseSearch 是我试图在派生函数 (ValueSearch) 中调用的基函数。我有问题的代码具体在 ValueSearch 下,调用 BaseSearch::Print(line, title)。 我得到的编译器错误是:我不确定我是否正确使用了继承。

Error   1   error C2275: 'std::string' : illegal use of this type as an expression

基础搜索.cpp

BaseSearch::BaseSearch()
{
}

BaseSearch::~BaseSearch()
{
}

void Print(string line, string title)
{
char c2 = '_'; //set character to blank value; defines the header row
char c = '_'; //set character to blank value; defines the searched row
int numHeader = 0; //sets the value of character for the header row
int numLine = 0; //sets the value of character for the searched row
c2 = line[numLine];
while (true) //force while loop
{
c = title[numHeader]; numHeader++; //header character is set to the title array defined as the entire header string above
if (c != ',')
{
cout << c; // couts the header until it reaches a ','
}
if (c == ',' || title.size() == numHeader) // if c reaches a ',' it will print the searched row
{
cout << ": ";
while (line.size() != numLine)
{
while (c2 != ',' && line.size() != numLine)
{
cout << line[numLine];
numLine++;
if (line.size() != numLine)
c2 = line[numLine];
else
break;
}
if (line.size() != numLine)
{
numLine++;
c2 = line[numLine];
cout << "\n";
break;
}
}

}

if (title.size() == numHeader) // if c reaches a null value, it breaks until other row is found.
{
cout << endl << endl; break;
}
}
}

基础搜索.h

#ifndef BASESEARCH_H
#define BASESEARCH_H
#include <string>

class BaseSearch
{
public:
BaseSearch();
virtual ~BaseSearch();
virtual void Print(string, string);
};

值搜索.cpp

ValueSearch::ValueSearch()
{
string input;
}

ValueSearch::~ValueSearch()
{
//dtor
}

double ValueSearch::getInput()
{
cout << endl << "Enter the name of the company you would like to search for: ";
cin >> input;
return input;
}

void ValueSearch::ValueSearchFunc(int c, int x)
{
column = c;

// cout << "Enter the name of the company you would like to search for: ";
// getline(cin, input);
string line;
ifstream fs("Stock Database.csv");
string title;
getline(fs, title);

while (!fs.eof())
{
getline(fs, line);
string companyname = ""; //start as blank, then append
string a;
int commacount = 0; //how many commas have we passed
int ChrCount = 0; //counter for which character in the line we are looking at

while (line != "\0") //while the line does not equal to null value.
{
double price;
price = 0;
a = line[ChrCount]; //convert char c to a string (a) so that we can append
ChrCount++;

if (a == ",")
{
commacount++; //increases the comma count as a encounters a comma each time.
}

else if (commacount == column && (a != "N" && a != "/" && a != "A")) //if comma count is equal to the set column, it will append the string company name.
{

while (a != ",")
{

if (a != ",")
{
companyname.append(a);
a = line[ChrCount];
ChrCount++;
}
}ChrCount--;
price = stod(companyname);
}

else if (commacount > column) // if the comma count is any value larger than the column, breaks out of loop.
{
break;
}
if (input == 0)
{
break;
}
if (x == 1){
if (price >= input && price != 0) // if the appended company name is equal to the search input entered, it will cout the entire row.
BaseSearch::Print(line, title);
}
if (x == 2)
{
if (price <= input && price != 0) // if the appended company name is equal to the search input entered, it will cout the entire row.
BaseSearch::Print(line, title);
}//end if

}
}
}

ValueSearch.h

#ifndef VALUESEARCH_H
#define VALUESEARCH_H
#include <string>
#include "BaseSearch.h"



class ValueSearch : public BaseSearch
{
public:
ValueSearch();
~ValueSearch();
double getInput();
void ValueSearchFunc(int c, int x);
void Print(string,string) {BaseSearch::Print(string,string);}

protected:
private:
double input;
int column;
};

#endif

最佳答案

看来你是C++的初学者。我将向你展示一个可以成功编译的示例代码。

基础搜索.h:

#ifndef BASESEARCH_H
#define BASESEARCH_H
#include <string>
using namespace std;

class BaseSearch
{
public:
BaseSearch();
~BaseSearch();
void Print(string, string);
};

#endif

基础搜索.cpp:

#include "BaseSearch.h"
#include <iostream>

BaseSearch::BaseSearch()
{
}

BaseSearch::~BaseSearch()
{
}

void BaseSearch::Print(string line, string title)
{
cout << "line:" << line << endl;
cout << "title:" << title << endl;
}

ValueSearch.h:

#ifndef VALUESEARCH_H
#define VALUESEARCH_H
#include <string>
#include "BaseSearch.h"

class ValueSearch : public BaseSearch
{
public:
ValueSearch();
~ValueSearch();
double getInput();
void ValueSearchFunc(int c, int x);
protected:
private:
double input;
int column;
};

#endif

值(value)搜索.cpp:

#include "ValueSearch.h"
ValueSearch::ValueSearch()
{

}

ValueSearch::~ValueSearch()
{

}

double ValueSearch::getInput()
{
return input;
}

void ValueSearch::ValueSearchFunc(int c, int x)
{
//where is 'input' from?

//if (x == 1)
//{
// if (price >= input && price != 0)
// BaseSearch::Print(line, title);
//}
//if (x == 2)
//{
// if (price <= input && price != 0)
// BaseSearch::Print(line, title);
//}//end if

}

但我不知道 ValueSearchFunc 想要做什么。

关于c++ - 从派生类函数调用基函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25419633/

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