【发布时间】:2017-08-08 19:59:19
【问题描述】:
我正在努力解决这个问题in Codility...
代码如下:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> &A, int k);
vector<int> A;
A.push_back(3);
A.push_back(5);
A.push_back(7);
A.push_back(9);
A.push_back(2);
int k;
rotate(A.rbegin(),A.rbegin()+k, A.rend());
虽然我的编译器编译和运行没有问题,但代码性告诉我“错误:'A' 没有命名类型”。 这是我的编译器用来检查它的代码:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> myVector;
myVector.push_back(3);
myVector.push_back(5);
myVector.push_back(7);
myVector.push_back(9);
myVector.push_back(2);
for(unsigned i=0;i<myVector.size();i++)
{
cout<<myVector[i]<<" ";
}
cout<<endl;
int k;
cout<<"Insert the times of right rotation:";
cin>>k;
rotate(myVector.rbegin(),myVector.rbegin()+k, myVector.rend());
for(unsigned i=0;i<myVector.size();i++)
{
cout<<myVector[i]<<" ";
}
}
编译器输出:
func.cpp:9:3: error: 'A' does not name a type
A.push_back(3);
^
func.cpp:10:3: error: 'A' does not name a type
A.push_back(5);
^
func.cpp:11:3: error: 'A' does not name a type
A.push_back(7);
^
func.cpp:12:3: error: 'A' does not name a type
A.push_back(9);
^
func.cpp:13:3: error: 'A' does not name a type
A.push_back(2);
^
func.cpp:16:9: error: expected constructor, destructor, or type conversion before '(' token
rotate(A.rbegin(),A.rbegin()+k, A.rend());
^
func.cpp:18:1: error: expected declaration before '}' token
}
^
Detected some errors.
【问题讨论】:
-
你忘了
main()吗? -
提示:使用
%操作符,如next_slot = (next_slot + 1) % total_slots; -
您显示错误消息
func.cpp:18:1: error: expected declaration before '}' token,但是,您的代码中没有这样的行。怎么可能?