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
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