Search code examples
c++bubble-sort

Why am I getting a <function> is not a member of <class>? I was trying to pass a string and return from it


I am trying to pass a string to a function, which is going to be sorted. Then to have the sorted string to be return from it. It won't compile, and it even says that "binarySort is not a member of Others".

Is it something that's not allowed to do?

This is the Class File

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

using namespace std;

char Others::inputCheck(char guess) {
    while (!isalpha(guess)) { //If the inputs are not alphabets, keep looping.
        cout << "Enter again: ";
        cin >> guess;
    }
    cin.ignore(1, '\n');
    guess = tolower(guess);

    return guess;
}

string Others::binarySort(string sampleText) {
    //Bubble sorting the string. (Copy pasted)
    for (int i = 0; i < sampleText.length(); i++)
    {
        for (int j = i + 1; j < sampleText.length(); j++)
        {
            if (sampleText[i] > sampleText[j]) //if previous has bigger ascii value than next,
            {
                //swapping the prev and next characters
                char temp = sampleText[i];
                sampleText[i] = sampleText[j];
                sampleText[j] = temp;
            }
        }
    }
    return sampleText;
}

This is the Header File.

#ifndef OTHERS_HEADER
#define OTHERS_HEADER
#pragma once
class Others
{
public:
    char inputCheck(char guess); //Function to check if inputs are alphabets.
    string binarySort(string sampleText);
};

#endif

The main function.

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

using namespace std;
int main() {
    string sortedText, sampleText = "toibeawn";
    Others sorter;
    sortedText = sorter.binarySort(sampleText);
    cout << "Text after sorted:\n";
    cout << sortedText;

    return 0;
}

The sort works if it's not used for a function.

The error output:

1>Sorting_test.cpp 1>Others.cpp 1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,7): error C2039: 'string': is not a member of 'std'

1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\predefined C++ types (compiler internal)(368): message : see declaration of 'std'

1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,24): error C3646: 'binarySort': unknown override specifier

1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,24): error C2059: syntax error: '(' 1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,30): error C2039: 'string': is not a member of 'std'

1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\predefined C++ types (compiler internal)(368): message : see declaration of 'std'

1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,48): error C2238: unexpected token(s) preceding ';'

1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.cpp(16,21): error C2039: 'binarySort': is not a member of 'Others'

1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(5): message : see declaration of 'Others' 1>Generating Code... 1>Done building project "Sorting_test.vcxproj" -- FAILED.


Solution

  • You include "Others.h" prior to declaring using namespace std which results in string being unrecognized in the included header.

    Add using namespace std; in the header "Others.h" and the error will be gone.

    Generally, it is a bad practice to have using namespace std in headers and you'd better just write std::string.