请教 VC++ error C2678 错误

tomotayuki 2011-09-05 05:38:20
VS2008下编译VC,在编译如下代码时出现C2678错误

a.h文件
class ClassA
{
....声明A类
}
typedef std::vector<ClassA> classArray;

ClassArray m_nClassArray;

a.cpp文件
void fun(const ClassA& classA)
{
classArray::iterator it;

for( it = m_nClassArray.begin(); it!= m_nClassArray.end(); it++ )

{
if(it == &classA)
{
continue;
}
}

}

编译错误
error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::_Vector_iterator<_Ty,_Alloc>' (or there is no acceptable conversion)
求高手!!!
...全文
300 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
TearyWang 2011-09-05
  • 打赏
  • 举报
回复
楼上的回答涵盖了问题所有的可能。最重要的两点:
1,你的参数是const&,传入的对象是const,而vector的iterator 有两种,一种是const_iterator,另一种是iterator,但无论是哪种,通常的参数均是const&,在const函数中,应该使用const_iterator,这里使用没有问题。
2. ==运算符的使用上,一种是对指针比较,指针比较时,是对两个地址进行“是否等于”的运算,而另一种是对对象比较,按照你的思路,这里的关键问题,应该是ClassA没有==运算符所至。

还有一种方法,你可以尝试void fun(const ClassA& classA),这里,你传递函数指针。也就是void fun(ClassA* pClassA)。
Kaile 2011-09-05
  • 打赏
  • 举报
回复
classA可能需要重载 == 操作符
  • 打赏
  • 举报
回复
在VC6下试了一下没问题
tomotayuki 2011-09-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 imlwj 的回复:]
C/C++ code

//The following is an example of this error:

class C
{
public: C();
};

void main()
{
C aC;
aC + 12; // error
}




binary 'operator' : no operator ……
[/Quote]
可以说的再详细些?
tomotayuki 2011-09-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fandh 的回复:]
if(*it == classA)
这样试试
[/Quote]
试了下,还是报同样的错误
imLWJ 2011-09-05
  • 打赏
  • 举报
回复

//The following is an example of this error:

class C
{
public: C();
};

void main()
{
C aC;
aC + 12; // error
}



binary 'operator' : no operator defined which takes a left-hand operand of type 'type' (or there is no acceptable conversion)

The specified operator was not defined for the specified type. To use the operator you must overload it for the apropriate type, or define a conversion to a type for which the operator is defined. If you’ve encountered this error on code which compiled with an earlier version of Visual C++, please read Technote: Improved Conformance to ANSI C++ for more information.
ccnunlp 2011-09-05
  • 打赏
  • 举报
回复
同意一楼的,把你的
if(it == &classA)//此行改为if(it == classA)
{
continue;
}
fandh 2011-09-05
  • 打赏
  • 举报
回复
if(*it == classA)
这样试试

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧