0

The class BaseSearch is the base function that I'm trying to call within the derived function (ValueSearch). The code that I have in question specifically is under ValueSearch, calling BaseSearch::Print(line, title). The compiler errors I'm getting are: I'm uncertain if I'm using inheritance correctly.

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

BaseSearch.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; } } } 

BaseSearch.h

#ifndef BASESEARCH_H #define BASESEARCH_H #include <string> class BaseSearch { public: BaseSearch(); virtual ~BaseSearch(); virtual void Print(string, string); }; 

Value Search.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 
1
  • You should always declare base class destructor 'virtual'. Look it up why. Why use inheritance if you're not defining any common interfaces (virtual functions)? And you haven't actually implemented any of the base class' function, hence the linker errors. Commented Aug 21, 2014 at 6:40

2 Answers 2

1

It seems you are a beginner of C++.I will show you an example code which will compile successfully.

BaseSearc.h:

#ifndef BASESEARCH_H #define BASESEARCH_H #include <string> using namespace std; class BaseSearch { public: BaseSearch(); ~BaseSearch(); void Print(string, string); }; #endif 

BaseSearch.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 

ValueSearch.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 } 

But I've no idea what ValueSearchFunc wants to do.

Sign up to request clarification or add additional context in comments.

2 Comments

I've extended the full code. Could you please take a quick glance?
ValueSearch.h:void Print(string str1, string str2) {BaseSearch::Print(str1, str2);} ValueSearch.cpp:#include <iostream> #include <fstream>
0

There is no realization of BaseSearch constructor/destructor in code. And Print function realization should be

void BaseSearch::Print(string line, string title) { //code } 

1 Comment

@user3867859 how about Print function right realization?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.