Thread: error C2065: undeclared identifier

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    13

    error C2065: undeclared identifier

    Hi,

    I'm having trouble with some code. I'm trying to reverse a string stored in a char array but I keep on getting the following errors:

    1>z:\visual studio 2008\projects\c++lab4test2\c++lab4test2\c++lab4tes t2.cpp(50) : error C2065: 'reverse' : undeclared identifier
    1>z:\visual studio 2008\projects\c++lab4test2\c++lab4test2\c++lab4tes t2.cpp(50) : error C2065: 'howMany' : undeclared identifier

    Code:
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    using namespace std;
    
    using std::cout;
    using std::endl;
    
    const int max_len = 300;
    class mystring
    
    {
    public:
    void reverseWord(char str [], char reverse [], int howMany);
    int length();
    void print();
    friend mystring operator+ (const mystring& a, const mystring& b);
    private:
    char s[max_len];
    int len;
    };
    
    // Member functions of the mystring class
    void mystring::reverseWord(char str [], char reverse [], int howMany)
    {
    
    for (int i = howMany -1,j=0; i>=0; i--,j++)
    reverse[j] = str[i];
    len = strlen(s);
    }
    int mystring::length()
    {
    return (len);
    }
    
    void mystring::print()
    {
    cout << s << "\nLength: " << len << "\n";
    }
    // Prototype of a general function (not a
    // member of the mystring class)
    void print(char* str);
    
    // The program’s main function
    
    int main()
    {
    char str[] = "My name is Charles Babbage.";
    mystring one;
    one.reverseWord(str, reverse, howMany);
    print(str);
    one.print();
    }
    I kow the problem is on this line:

    one.reverseWord(str, reverse, howMany);

    But if I remove reverse and howMany the I get this error:

    error C2660: 'mystring::reverseWord' : function does not take 1 arguments

    Apologies about the code being a mess, still struggling with the language.

  2. #2
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    next time please take out the unnecessary code before posting. also you should read up on classes and passing pointers, passing variables by value and by reference. comes in very handy for code like this

    There you go

    Code:
    #include <iostream>
    #include <string>
    #include <string.h> //Code::Blocks seems to need it -__-
    
    using namespace std;
    
    const int max_len = 300;
    class mystring
    
    {
    public:
        char * reverseWord(char * str);
        int length(char * str);
        void print();
        friend mystring operator+ (const mystring& a, const mystring& b);
    private:
        char s[max_len];
        int len;
    };
    
    // Member functions of the mystring class
    char * mystring::reverseWord(char * str)
    {
        len = this->length(str);
        char * reverse = new char[len];
        for (int i = len - 1,j=0; i >= 0; i--,j++)
            reverse[j] = str[i];
        reverse[len] = '\0';
        return reverse;
    }
    
    int mystring::length(char * str)
    {
        int len = 0;
        while (*(str++))
            len++;
        return len;
    }
    
    void mystring::print()
    {
        cout << s << "\nLength: " << len << "\n";
    }
    // Prototype of a general function (not a
    // member of the mystring class)
    //void print(char* str);
    
    // The program’s main function
    
    int main()
    {
        char str[] = "My name is Charles Babbage.", *reverse;
        mystring one;
        reverse = one.reverseWord(&str[0]);
        cout << reverse << endl;
        //one.reverseWord(str, reverse, howMany);
        //print(str);
        //one.print();
    }
    Last edited by codeprada; 02-14-2011 at 01:35 PM.
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    Will do, theres definately a lot of stuff I need to read up on! Thanks codeprada

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling Libraries in C
    By TheOriginalGame in forum C Programming
    Replies: 3
    Last Post: 08-15-2010, 11:19 AM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM