Search code examples
c++modularization

CharConverter unknown errors


I'm creating a program here are the specifications:

Create a CharConverter class that performs various operations on strings. It should have the following two public member functions to start with.

The uppercase member function accepts a string and returns a copy of it with all lowercase letters converted to uppercase. If a character is already uppercases or is not a letter, it should be left alone.

The properWords member function accepts a string of words separated by spaces and returns a copy of it with the first letter of each word converted to uppercase.

Write a simple program that uses the class. It should prompt the user to input a string. Then it should call the properWords function and display this resulting string. Finally, it should call the uppercase function and display this resulting string.

I wrote the program without modularizing it to make sure I converted everything right. Now that I'm trying to modularize I'm getting errors I don't know what they mean, when compiled:

errors

here is my code:

#include<iostream>
#include<string>
#include<vector>
#include<ctype.h>

using namespace std;

class CharConverter {

public:
void uppercase(string, int);
void properWords(string, int);

};

void CharConverter::uppercase(string myString, int s) {

s = myString.length();

for (int i = 0; i <= s; i++) {

    myString[i] = toupper(myString[i]);

}

cout << myString << endl;

}

void CharConverter::properWords(string myString, int s) {

for (int i = 0; i <= s; i++) {
    myString[i];

    myString[0] = toupper(myString[0]);

    if (myString[i] == ' ') {
        myString[i + 1] = toupper(myString[i + 1]);
    }
}

cout << myString << endl;
}


int main() {

void properWords(string, int);
void uppercase(string, int);


string sentence;
int size;

cout << "Enter a sentence you want converted to all uppercase letters and 
set up with proper uppercase letters." << endl;
getline(cin, sentence);

size = sentence.length();

properWords(sentence, size);

uppercase(sentence, size);

return 0;
}

Solution

  • In the beginning of main

    int main() {
    
    void properWords(string, int);
    void uppercase(string, int);
    

    you declare two other functions, not part of CharConverter. And then you call those functions, not the ones you defined earlier.

    And so the compiler (linker actually) complains that it cannot find these non-member functions. And that is true, they don't exist.

    Now you have to decide if you need a class. In that case create a class object and call the member functions. Or skip the class declaration and make the functions free functions.