注册 登录
编程论坛 新人交流区

用Dev-Cpp编译时出现的警告问题

TheEleven 发布于 2007-11-05 15:09, 441 次点击
我写的一个小程序里面,发现只要是写了如下的代码:
vector<Student_info> students, fails;

......

fails = extract_fails(students);


用Dev-Cpp(Gcc3.4.5)编译时,就会提示如下警告(注,是加-Wall参数才会出现的警告,不加则没有):
C:\Dev-Cpp\include\c++\3.4.5\bits\stl_vector.h In member function `std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = Student_info, _Alloc = std::allocator<Student_info>]':

715 C:\Dev-Cpp\include\c++\3.4.5\bits\stl_vector.h [Warning] '__result' might be used uninitialized in this function

82 C:\Dev-Cpp\include\c++\3.4.5\bits\stl_uninitialized.h [Warning] '__cur' might be used uninitialized in this function

82 C:\Dev-Cpp\include\c++\3.4.5\bits\stl_uninitialized.h [Warning] '__cur' might be used uninitialized in this function

一旦我将代码改写成如下的形式:
vector<Student_info> students;
......
vector<Student_info> fails(extract_fails(students));

就不会警告了,这个是什么原因造成的呢?

3 回复
#2
TheEleven2007-11-05 15:10

为了验证,写了个最简单的小程序,结果发现内置类型似乎就可以这样复制元素,而库类型就不成了:
[CODE]#include <iostream>
#include <string>
#include <vector>

using std::cout; using std::endl;
using std::vector; using std::string;

int main(){ vector<int> v1, v2;
v1.push_back(123);
v2 = v1;
cout << v1[0] << endl << v2[0] << endl;

vector<string> s1, s2;
s1.push_back("test");
s2 = s1;
cout << s1[0] << endl << s2[0] << endl;

system("pause");
return 0;
}[/CODE]
把s1、s2那部分注释掉,编译时就不会警告了

#3
TheEleven2007-11-05 15:11
我看书上都有那种“v1 = v2”,即“用容器v2的内容的副本取代v1容器的内容”

所以这么写了,结果就报警告
#4
TheEleven2007-11-06 12:08
都没人回复啊。。。。。
1