1

I have two cpp files and one hpp file. Main.cpp, Ab.cpp and Ab.hpp.

In these files I have created a class 'Ab' that has a default constructor and a constructor that takes a string. Within the class I want to redefine the * operator to set a given value to an object of the class and also delete any previous value that was assigned to it.

It is with mentioning that I have been instructed that I am not allowed to use any copy constructor or copy assignemnt in this task. Meaning I have to resort to using purely move constructor and move assignemnt. And in these subjects my knowledge is very limited as I've only worked with basic C# before.

The Main.cpp is as following:

#include <iostream>
#include "Ab.hpp"

A MoveTest(std::string testData)
{
    return Ab(new std::string(testData));
}

int main()
{
    std::cout << "-----'Ab' Test Begin-----" << std::endl;


    std::cout << "'Ab' test: Constructor begins." << std::endl;
    Ab emptyAb;
    Ab moveTestAb(new std::string("To remove"));
    std::cout << "'Ab' test: Constructor done. Press enter to continue." << std::endl;
    std::cin.get();

    std::cout << "Ab' test: Moveoperator begins." << std::endl;
    moveTestAb = MoveTest("This is a test movement");
    std::cout << "Expected output:         " << "This is a test movement" << std::endl;
    std::cout << "Output from moveTestAb: " << *moveTestAb << std::endl;
    std::cout << "'Ab' test: Moveoperator done. Press enter to continue." << std::endl;
    std::cin.get();
    std::cout << "-----'Ab' Test End-----" << std::endl;
    std::cin.get();
}

The Ab.cpp is as following:

#include "Ab.hpp"

std::string Ab::Get() const
{
    return "test";
}
bool Ab::Check() const
{
    bool return_value = true;
    if (this==NULL)
    {
        return_value = false;
    }
    return return_value;
}

Ab & Ab::operator=(const Ab &ptr)
{
    return *this;
}


Ab & Ab::operator*(Ab &other)
{
    if (this != &other) {
        delete this->a_string;
        this->a_string = other.a_string;
        other.a_string = nullptr;
    }
    Ab *thing_to_return = &Ab(this->a_string);
    return *thing_to_return;  
}

The Ab.hpp is as following

#include <string>
class Ab
{
    Ab(const Ab&) = delete;

    std::string* a_string;
    public:
        Ab &operator=(const Ab&);

    Ab& operator*(Ab&);



        Ab();
        Ab(std::string *the_string):
        a_string(the_string){};
        int b = 0;
        int a = 3;
        std::string Get() const;
        ~Ab() = default;
        bool Check() const;

    private:
        int z = 0;
};

I am currently getting the error:

no operator "*" matches these operands -- operand types are: * AB

6
  • 1
    What do you want *moveTestAb to do? Commented Dec 11, 2018 at 20:47
  • I just as I posted it realized that might be a little unclear as I was give most of the code and didn't write all of it myself. In this case I want it to get the string value of the variable a_string points to and return it. Commented Dec 11, 2018 at 20:49
  • That makes sense. What about your definition of Ab & Ab::operator*(Ab &other) makes you think it should behave this way? (I'm trying to find out where your misunderstanding happened) Commented Dec 11, 2018 at 20:57
  • That is some code that I found and made modifications to so that it would match a move assignment. My first atempt was to just get the string it was holding at return that. Then I I tried returning this->a_string. Then after browsing forums for a few hours a post said that construction that override would help. Commented Dec 11, 2018 at 21:05
  • 1
    when you call *moveTestAb the compiler will search for a function matching the signature of <return_type> Ab::operator*() <optional_const>. Notice how the function doesn't take any parameters. Commented Dec 11, 2018 at 21:08

2 Answers 2

0
Ab& operator*(Ab&);

This does not allow you to do *ab. This operator is called when you do ab*ab; https://gcc.godbolt.org/z/SDkcgl

Ab *thing_to_return = &Ab(this->a_string);

here you are taking the pointer to a temporary. There are more issues with your code. I suggest to rewrite step by step

Sign up to request clarification or add additional context in comments.

Comments

0

The problem was solved using the comments from AndyG that he wrote on the origial question.

Comment containing solution:

when you call moveTestAb the compiler will search for a function matching the signature of Ab::operator() . Notice how the function doesn't take any parameters - AndyG

1 Comment

I'll get right on that by editing this post to contain the comments that he wrote and then also mark it as solved. I had to wait one day before being able to mark my own answer as a solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.