CharConverter 未知错误

CharConverter unknown errors

我正在创建一个程序,这里有规格:

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.

我编写程序时没有将其模块化以确保我正确转换了所有内容。现在我正在尝试模块化,但在编译时出现错误,我不知道它们是什么意思:

这是我的代码:

#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;
}

main开头

int main() {

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

您声明了两个 other 函数,而不是 CharConverter 的一部分。然后你调用那些函数,而不是你之前定义的函数。

因此编译器(实际上是链接器)抱怨它找不到这些非成员函数。没错,它们并不存在。

现在您必须决定是否需要 class。在这种情况下,创建一个 class 对象并调用成员函数。或者跳过 class 声明并使函数成为自由函数。