简体   繁体   English

函数不接受 0 个参数

[英]Function does not take 0 Arguments

The uppercase() function is part of my class: uppercase() 函数是我班级的一部分:

class MyString {
public:
    MyString();
    MyString(char cstring[]);
    void uppercase();

private

};

And the implementation for uppercase is not finished, but looks like this:并且大写的实现还没有完成,但是看起来像这样:

void MyString::uppercase()
{
    cout << "need to implement";
}

When I call the function, it looks like this:当我调用该函数时,它看起来像这样:

//Output streaming Operator Overload
ostream& operator<<(ostream& os, const MyString& string)
{
    if (MyString::printAsUppercase == true)
        uppercase();
    else
        os << string.data;

    cout << "(" << string.length << ")";
    return os;

}

When I attempt to compile the code, I receive the following error:当我尝试编译代码时,收到以下错误:

'std::uppercase': function does not take 0 arguments

I really don't understand this, as I declared the prototype to NOT take any arguments, and followed through with that in the implementation.我真的不明白这一点,因为我声明原型不接受任何参数,并在实现中跟进。 The function shouldn't have to take any arguments.该函数不应该接受任何参数。 Why does this happen?为什么会发生这种情况?

std::uppercase is a legitimate I/O stream object in the standard library. std::uppercase是标准库中合法的 I/O 流对象。 Because you are coding in the std namespace, the compiler cannot tell if you are using your function or the library object.因为您在 std 命名空间中编码,所以编译器无法判断您使用的是函数还是库对象。 One simple solution is change the name of your function or specify your class MyClass::uppercase(...).一种简单的解决方案是更改函数名称或指定类 MyClass::uppercase(...)。 A better solution would be to wrap your code in your own namespace or avoid coding in the std namespace with the declaration "using namespace std;"更好的解决方案是将您的代码包装在您自己的命名空间中,或者避免在 std 命名空间中使用声明“using namespace std;”进行编码。 In my opinion, it more clear to specify the std namespace each time it is used.在我看来,每次使用时都指定 std 命名空间更清楚。 For example, instead of writing "using namespace std;"例如,而不是写“使用命名空间 std;” then "cout << ..." just write "std::cout << ..."然后 "cout << ..." 只写 "std::cout << ..."

You need to specify that you want to call your uppercase method.您需要指定要调用大写方法。 The Error states that you want to call "std::uppercase" << std:: you need to call your method on your class.错误指出您要调用“std::uppercase”<<std:: 您需要在您的类上调用您的方法。

Comment edit: François Andrieux mentioned that this is likely a problem because you use "using namespace std;"评论编辑:François Andrieux 提到这可能是一个问题,因为您使用了“using namespace std;” Therefore all function calls will be searched in the std.因此,所有函数调用都将在 std 中搜索。 (And hes right with that :) ) (他说得对:))

I hope the following code could help understanding the points indicated by other users.我希望以下代码可以帮助理解其他用户指出的要点。 It compiles, but... definitely it's far from optimal:它可以编译,但是......绝对不是最佳的:

main.cpp

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

int main(int argc, char** argv)
{
   // Needs arguments from command line!!!
   MyString teststring(argv[1]);
   std::cout << teststring << std::endl;
   return 0;
}

MyString.h

class MyString {
public:
   MyString();
   MyString(char *cText);
   void myUpperCase();
   int myStringLenght();

protected:
   bool printAsUppercase;
   char *data;
   friend std::ostream& operator<<(std::ostream& os, 
                                    MyString mystring);
};

MyString.cpp

#include <cstdlib>
#include <cstring>
#include <iostream>
#include "MyString.h"

MyString::MyString(char *cText)
{
   data = (char *)std::malloc(strlen(cText));
   std::strcpy(data, cText);
}

void MyString::myUpperCase()
{
   std::cout << "not yet implemented";
}

int MyString::myStringLenght()
{
   return int(strlen(data));
}

std::ostream& operator<<(std::ostream& os, 
                           MyString mystring)
{
   // to be deleted - only for testing
   mystring.printAsUppercase = true;

   if (mystring.printAsUppercase == true)
      mystring.myUpperCase();
   else
      os << mystring.data;

   std::cout << "(" << mystring.myStringLenght() << ")";
   return os;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM