0

I'm trying to use cstring. I have both

#include <cstring>

#include <string>

in my class in progress.

When I try to compile, GNU gives messages that what I believe are the functions of the class have not been declared. For example, "error: '::memchr' has not been declared

What should I do to use this file properly?

Edit: Adding/explaining my code

The project is to make a String class to provide functions such as addition and comparison to strings (though the regular string class is also required). I suspect the problem has to do with a collision between my String class and the library string class, so I'm looking at changing the #ifndef and #define lines.
String.h Code:

  #ifndef STRING_H
  #define STRING_H

//#ifndef MYSTRING_H //experiment
//#define STRING_H   //experiment

#include <string>
#include <iostream>


using namespace std;


class String
{
    public:
        String();
        //String(const char*);
        String(char const*); //
        String(char);
        String(int);
        String(const String&);
        String(char, int);

        void print();
        int getLength();
        String substr(int, int);

        //overloads
        String& operator=(const String&);
        String& operator=(const char*);

        friend String operator+(const String&, const String&);
        friend String operator+(const String&, const char*); 
        friend String operator+(const char*, const String&);
        friend String operator+(const String&, char);
        friend String operator+(char, const String&);
        String operator+=(const String&);
        String operator+() const;
        friend int operator==(const String&, const String&);
        friend int operator!=(const String&, const String&);
        friend int operator< (const String&, const String&);
        friend int operator<=(const String&, const String&);
        friend int operator> (const String&, const String&);
        friend int operator>=(const String&, const String&);
        char& operator[](int);
        friend char* operator+(const String&, int);
        friend char* operator+(int, const String&);
        String& operator++(); //prefix
        String& operator--(); //prefix
        String operator++(int); //postfix
        String operator--(int); //postfix
        friend ostream& operator<<(ostream&, const String&);

        //virtual ~String();
    protected:
    private:
      int length;
      char* buf;
};

#endif // STRING_H

String.cpp Code:

#include <cstring>
#include <iostream>
#include "String.h"
#include <string>



using namespace std;

String::String()
{
    //ctor
    //cout <<"default ctor called" << endl;
}

String::String(char const*)
{

}

String::String(char)
{

}

String::String(int)
{

}

String::String(String const&)
{

}

String::String(char, int)
{

}

void String::print()
{
    cout << "printing something " << endl;
}

String String::substr(int, int)
{

}

int String::getLength()
{
    string h = "hi";
    //std::strlen(h);
    return length;
}

String& String::operator=(const String&)
{

}

String& String::operator=(const char*)
{

}

String operator+(const char*, const String&)
{

}

String operator+(const String&, const char*)
{

}

String operator+(const String&, const String&)
{

}

String operator+(const String&, char)
{

}

String operator+(char, const String&)
{

}

String String::operator+=(const String&)
{

}

String String::operator+() const
{

}

int operator!=(const String&, const String&)
{

}

int operator==(const String&, const String&)
{

}

int operator< (const String&, const String&)
{

}

int operator<=(const String&, const String&)
{

}

int operator> (const String&, const String&)
{

}

int operator>=(const String&, const String&)
{

}

char& String::operator[](int)
{

}

char* operator+(const String&, int)
{

}

char* operator+(int, const String&)
{

}

String& String::operator++()
{

}

String& String::operator--()
{

}

String String::operator++(int)
{

}

String String::operator--(int)
{

}

ostream& operator<<(ostream&, const String&)
{

}

//String::~String()
//{
//    //dtor
//}
Rez
  • 187
  • 12
  • Do you have `#include `, or `include cstring`? If you have `#include `, then when writing your post, click the little question mark in the upper right of the editor box to learn about how to make stuff like that show up properly. – user2357112 Aug 02 '15 at 00:43
  • I do have pound; I just didn't write it here at first because I didn't get the formatting of this site. sorry, new to how this works. – Rez Aug 02 '15 at 00:45
  • Is there another message like `cstring: No such file or directory` or `no include path in which to search for cstring`? – Potatoswatter Aug 02 '15 at 01:32
  • No, just "x method has not been declared" all 22 times. – Rez Aug 02 '15 at 01:42

2 Answers2

0

Most of the things in the standard library are in the std namespace in C++, including functions like memchr. You need to either call it as std::memchr, or declare something like using std::memchr; to call it as memchr instead of std::memchr. (There's also using namespace std;, but that's a bad idea.)

Community
  • 1
  • 1
user2357112
  • 260,549
  • 28
  • 431
  • 505
0

The <cxxx> libraries do not necessarily declare names in the global namespace like ::name. (They often do, though.)

You need to call it as std::memchr.

Other alternatives allowing ::memchr are to use #include <string.h> or write using namespace std; (yuck!), but really it's best to put std:: in front of standard library names.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • Well, I tried std::strlen() and now GNU is saying strlen() is not a member of std, and I know strlen() is part of . – Rez Aug 02 '15 at 01:07
  • Yeah, it must be a problem with my project specifically because I didn't get any of these errors when I put cstring on a simple hello world. – Rez Aug 02 '15 at 01:36
  • @Rez: If the compiler is saying that `strlen` is not a member of `std`, we'd need to see the *exact* source code that produces the error. – Keith Thompson Aug 02 '15 at 04:40
  • @Keith Thompson: I just added the class with this problem. – Rez Aug 02 '15 at 05:08
  • @Rez: You have: `//std::strlen(h);`. That's a comment, so obviously the compiler isn't going to complain about it. You have `#include `, so the compiler shouldn't complain even if you uncomment the `strlen` reference. If you want to know why the compiler is complaining that `strlen()` is not a member of `std`, you need to show us the *exact* copy-and-pasted source that the compiler actually complains about (along with the exact copy-and-pasted error message). But it's probably a dupe. `#include ` puts `strlen` in the `std` namespace; `#include ` may or may not. – Keith Thompson Aug 04 '15 at 19:14