Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ / Operator overloading / Compiler Error

Options
  • 29-11-2002 4:36pm
    #1
    Registered Users Posts: 2,281 ✭✭✭


    This syntax error has two friends and myslef absolutely stumped. The three of us are in third year computer science and we cant fix a c++ syntax error.

    :rolleyes:

    String.h
    [php]#ifndef __String__
    #define __String__

    namespace marc
    {

    class String
    {
    private:
    int len; // Length of the String
    char* data; // Pointer to the array of characters

    public:
    String (void); // Default constructor
    String (const char* s); // Character array constructor
    String (const String &other); // Copy constructor
    ~String (void); // Destructor

    private:
    String (const String &one, const String &two); // Concat Constructor

    public:

    operator char*() const; // Cast this to a char pointer

    String operator = (const String &other) const;
    String operator + (const String &other) const;
    bool operator == (const String &other) const;
    bool operator < (const String &other) const;
    bool operator <= (const String &other) const;
    bool operator > (const String &other) const;
    bool operator >= (const String &other) const;

    bool valid_index (const int i) const;
    int index_of (const char c, const int start_index = 0) const;
    bool has (const char c) const;
    void clear (void);

    };
    }

    #endif
    [/php]

    String.cpp
    [php]
    #include <string.h>
    #include "String.h"
    #include <iostream.h>

    using namespace marc;

    String::String (void)
    {
    this->len = 0;
    this->data = new char[1];
    this->data[0] = 0;
    }

    String::String (const char* s)
    {
    this->len = strlen(s);
    this->data = new char [len+1];
    for (int i = 0; i < this->len; i++)
    {
    this->data = s;
    }
    this->data[len] = 0;
    }


    String::String (const String &other)
    {
    this->len = other.len;
    this->data = new char[len+1];
    for (int i = 0; i < this->len; i++)
    {
    this->data = other.data;
    }
    this->data[len] = 0;
    }


    String::String (const String &one, const String &two)
    {
    int i;
    this->len = one.len + two.len;
    this->data = new char[this->len + 1];

    for (i = 0; i < one.len; i++)
    {
    this->data = one.data;
    }
    for (; i < this->len; i++)
    {
    this->data = two.data;
    }
    }

    String::~String (void)
    {
    delete [] this->data;
    }


    bool String::valid_index (const int i) const
    {
    return (0 <= i) && (i < len);
    }


    int String::index_of (const char c, const int start_index = 0) const
    {
    for (int i = start_index; i < len; i++)
    {
    if (this->data == c) return i;
    }
    }

    void String::clear (void)
    {
    for (int i = 0; i <= len; i++)
    {
    this->data = 0;
    }
    }

    bool String::has (const char c) const
    {
    for (int i = 0; i < this->len; i++)
    {
    if (data == c) return true;
    }
    return false;
    }

    String::operator char*() const
    {
    return this->data;
    }

    String String::operator = (const String& other) const
    {
    return String(other);
    }

    String String::operator + (const String& other) const
    {
    return String(*this, other);
    }

    bool String::operator ==(const String& other) const
    {
    return (strcmp(this->data, other.data) == 0);
    }

    bool String::operator < (const String& other) const
    {
    return (strcmp(this->data, other.data) == -1);
    }

    bool String::operator <= (const String& other) const
    {
    return (strcmp(this->data, other.data) < 1);
    }

    bool String::operator > (const String& other) const
    {
    return (strcmp(this->data, other.data) == 1);
    }

    bool String::operator >= (const String& other) const
    {
    return (strcmp(this->data, other.data) > -1);
    }

    bool unit_test(void)
    {
    String s1 ();
    String s2 ("Hello world");
    String s3 ("Hello world");
    String s4 ("Hello nurse");

    bool OperatorEqualsEquals = (s2 == s3);
    return OperatorEqualsEquals;
    }

    using namespace std;

    int main(void)
    {
    marc::String passed("Passed");
    marc::String failed("Failed");

    cout << "Class String test: ";
    unit_test() ? cout << passed : cout << failed;
    cout << endl << flush;

    return 1;
    }

    [/php]

    Compiler error:

    3:29pm dbc@matrix [~/files]> g++ -o String String.cpp
    String.cpp: In function `bool unit_test()':
    String.cpp:138: no matching function for call to
    `marc::String::operator==(marc::String (&)())'
    String.cpp:107: candidates are: bool marc::String::operator==(const
    marc::String&) const


    Can anyone see why it wont let me use the == operator?


Comments

  • Registered Users Posts: 1,481 ✭✭✭satchmo


    I think it's your namespaces are screwing things up. I tried it without all the namespace stuff and it compiled & ran fine (unit_test passed). I don't use namespaces though so past telling you what the problem is, I can't really offer any solution. :(

    [edit] On second try, I put back in the namespace stuff, but it was complaining that 'std : does not exist or is not a namespace', so i took out the line using namespace std and it worked fine :confused:[/edit]


  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    Perhaps
    marc::String:: operator ==
    
    would work in that case.


  • Registered Users Posts: 2,281 ✭✭✭DeadBankClerk


    I have founf the error. I was makeing a marc::String object s1 by declaring it

    String s1 ();

    I should have been declaring:
    String s1;

    The empty parenthesis dont call the correct constructor apperently :/

    I have fixed it now, although i wasted an hour of my life :)


  • Closed Accounts Posts: 6,718 ✭✭✭SkepticOne


    I copied the code and compiled but got completely different errors:
    [b]$ g++ String.cpp -o String[/b]
    
    String.cpp:133: default argument given for parameter 2 of `int 
       marc::String::index_of(char, int = 0) const'
    String.h:69: after previous specification in `int marc::String::index_of(char, 
       int = 0) const'
    
    This was fixed by removing the default argument in the implemetation of String::index_of(... in String.cpp and leaving it in the declaration in the header file.

    Following that I get the output:

    "Class String test: Passed"

    I'm using cpp-3.2.1, btw. This apparently is more standards complient than older versions.


Advertisement