-1

这是代码:

#include <string>
#include <iostream>

#include <vector>
#include <algorithm>

using namespace std;

struct Pair{
  string name;
  double val;
};

vector<Pair> pairs;

double& value(const string& s)
{
  for(int i=0; i < pairs.size(); i++)
    if(s == pairs[i].name)
      return pairs[i].val;

  Pair p = {s, 0};
  pairs.push_back(p);

  return pairs[pairs.size() - 1].val;
}

int main()
{
  string buf;
  while(cin>>buf) value(buf)++;

  for(vector<Pair>::const_iterator p = pairs.begin(); p != pairs.end(); ++p)
    cout << p->name << ": " << p->val << '\n';
  return 0;
}

编译器总是抱怨 Line (Pair p = {s, 0};) 错误:
Borland C++ 5.5.1 for Win32 版权所有 (c) 1993, 2000 Borland
....\cpp\TEST.CPP:
Warning W8012 .. ..\cpp\TEST.CPP 18: 比较函数值中的有符号和无符号值
(const string &)
错误 E2291 ....\cpp\TEST.CPP 22: } 预期在函数值中(const string &)
错误 E2034 ....\cpp\TEST.CPP 22:无法在
函数值中将“const string”转换为“Pair”(const string &)
错误 E2141 ....\cpp\TEST.CPP 22:函数中的声明语法错误value( const
string &)
Error E2139 ....\cpp\TEST.CPP 22: Declaration missing ; 在函数值中(常量
字符串 &)
警告 W8070 ....\cpp\TEST.CPP 22: 函数应该在函数值中返回一个值
(const string &)
警告 W8004 ....\cpp\TEST.CPP 22: 'p' 被赋值为从未
在函数值中使用过(const string &)
Error E2190 ....\cpp\TEST.CPP 22: Unexpected }
Error E2238 ....\cpp\TEST.CPP 23: Multiple declaration for 'pairs'
Error E2344 .. ..\cpp\TEST.CPP 14:“对”的早期声明
错误 E2141 ....\cpp\TEST.CPP 23:声明语法错误
错误 E2040 ....\cpp\TEST.CPP 25:声明错误终止
错误 E2190 ....\cpp\TEST.CPP 26: Unexpected }
* 10 个编译错误 *

4

1 回答 1

1

该代码使用最新的编译器 (GCC 4.6.3) 编译时仅出现警告。您的编译器不能完全兼容。

有关 GCC 4.3.4 的编译,请参见此处

于 2012-09-09T14:06:46.457 回答