У меня есть два файла cpp и один файл hpp.Main.cpp, Ab.cpp и Ab.hpp.
В этих файлах я создал класс Ab с конструктором по умолчанию и конструктором, который принимает строку.Внутри класса я хочу переопределить оператор *, чтобы установить заданное значение для объекта класса, а также удалить все предыдущие значения, которые были ему присвоены.
Это с упоминанием, что я был проинструктирован, что яМне не разрешается использовать любой конструктор копирования или назначить копию в этой задаче.Это означает, что я должен прибегнуть к использованию чисто конструктора перемещения и перемещения ассемблера.И по этим предметам мои знания очень ограничены, так как раньше я работал только с базовым C #.
Main.cpp выглядит следующим образом:
#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();
}
Ab.cpp выглядит следующим образом:
#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;
}
Ab.hpp выглядит следующим образом
#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;
};
В настоящее время я получаю сообщение об ошибке:
ни один оператор "*" не соответствует этим операндам- типы операндов: * AB