简体   繁体   中英

Function does not take 0 Arguments

The uppercase() function is part of my class:

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. Because you are coding in the std namespace, the compiler cannot tell if you are using your function or the library object. One simple solution is change the name of your function or specify your class 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;" In my opinion, it more clear to specify the std namespace each time it is used. For example, instead of writing "using namespace std;" then "cout << ..." just write "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.

Comment edit: François Andrieux mentioned that this is likely a problem because you use "using namespace std;" Therefore all function calls will be searched in the 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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