代码是未完成的但是从我到目前为止我一直得到一个错误,指出“没有运算符'='匹配这些操作数” [英] The code is unfinished but from what I have so far I keep getting an error stating that "no operator '=' matches these operands"

查看:70
本文介绍了代码是未完成的但是从我到目前为止我一直得到一个错误,指出“没有运算符'='匹配这些操作数”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include <string>
using namespace std;

class Movie
{
private:
	string name;
	string director;
public:
	Movie()
	{
		name = "Movie";
		director = "Director";
	}
	Movie(string n, string d)
	{
		name = n;
		director = d;
	}
	void setName(string n)
	{
		name = n;
	}
	void setDirector(string d)
	{
		director = d;
	}
	string getName()
	{
		return name;
	}
	string getDirector()
	{
		return director;
	}
	void DisplayData(Movie data[], int size)
	{
		cout << "Movie list: ";
		for (int c = 0; c < size; c++)
		{
			cout << name[c] << " ";
		}

		cout << endl;
	}
	void BubbleSort(Movie data[], int size)
	{
		string temp;
		bool swap;
		do
		{
			swap = false;
			for (int c = 0; c < size - 1; c++)
			{
				if (data[c].getName() > data[c+1].getName())
				{
					temp = data[c];
				}
			}
		} while (swap = true);
	}

};





我的尝试:



我尝试过不同的东西,但无济于事。



What I have tried:

I've tried different things but to no avail.

推荐答案

{
    temp = data[c];
}





你试图将'temp'(一个字符串)分配给一个电影的实例类。您需要将其分配给类中name字符串的副本。你可以通过为类创建一个'='运算符,或者通过'getName'或其他方式创建。



You are trying to assign 'temp' (a string) to an instance of a Movie class. You need to assign it to a copy of the 'name' string in the class. You can do so by creating an '=' operator for the class, or through 'getName' or another way if you wish.


你的陈述错误:

Your while statement is wrong:
do
		{
			swap = false;
			for (int c = 0; c < size - 1; c++)
			{
				if (data[c].getName() > data[c+1].getName())
				{
					temp = data[c];
				}
			}
		} while (swap = true);



你要分配 true swap ,而不进行比较。 '等于'是==,而不是=。


You're assigning true to swap, not making a comparison. 'Equal to' is ==, not =.


嘿,这是C ++,你可以更好地替代数组和冒泡排序。

Hey man, it is C++ and you far better alternatives to arrays and bubble sort.
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

class Movie
{
private:
  string name;
  string director;
public:

  Movie(string n="Movie", string d="Director"):name(n), director(d){}

  void setName(string n)
  {
    name = n;
  }
  void setDirector(string d)
  {
    director = d;
  }
  const   string getName() const
  {
    return name;
  }
  const string getDirector() const
  {
    return director;
  }
};

ostream & operator << (ostream & os, const Movie & movie)
{
  os << movie.getName();
  return os;
}

int main()
{
  vector< Movie > mv{ Movie("Foo", "boo"), Movie("Goo"), Movie("AAA", "BBB")};

  cout << "list of movies: ";
  for ( const auto & m : mv)
    cout << m << " ";
  cout << endl;

  sort( mv.begin(), mv.end(), [](const Movie & m1, const Movie & m2) {return (m1.getName() < m2.getName());});

  cout << "sorted list of movies: ";
  for ( const auto & m : mv)
    cout << m << " ";
  cout << endl;
}


这篇关于代码是未完成的但是从我到目前为止我一直得到一个错误,指出“没有运算符'='匹配这些操作数”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆