c++ - CharConverter 未知错误

标签 c++ modularization

我正在创建一个程序,这里是规范:

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.

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

errors

这是我的代码:

#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 的一部分。然后调用这些函数,而不是之前定义的函数。

因此编译器(实际上是链接器)提示它找不到这些非成员函数。这是真的,它们不存在。

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

关于c++ - CharConverter 未知错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47255451/

相关文章:

c++ - "char fun[](int x) const"是做什么的? : C++ Syntax Error

c++ - Opencv ROI 比它应该填充的更多

javascript - Kendo UI 与 Javascript 模块化模式

java - 无法调用不同模块中的类(动态功能模块)

c++ - 事件类需要更新

c++ - 使用 Boost 库程序选项的必需和可选参数

c++ - XP 滚动条在 Windows7/Vista 中乱七八糟

architecture - 软件项目的模块化有多重要

node.js - 如何模块化 AngularJS 应用程序/插件