我创建了这个程序,出于某种原因,Visual Studio 给出了这个“警告”(“预处理器指令后出现意外标记 - 需要换行符”),同时还发现错误似乎引用了该行下一行的内容编译器声称他们在。瞬间,错误Carat Error似乎指的是只适用的东西one line down .因此,我相信程序的 header 中一定出现了严重错误,但我不确定。这是代码:
//1/4/2017
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
void getNewItem();
void displayItems();
void displayRand();
vector<string> vecItems;
int main()
{
//declaration phase
int intInput;
string strNewItem;
cout << "Random Item Generator" << endl << "Written by #XXXXX" << endl << "1. Add Item" << endl << "2. Display All Items " << endl << "3. Display Random Item" << endl << "4. Quit" << endl;
while (intInput != 4)
{
cin >> intInput;
switch (intInput)
{
case 1:
getNewItem();
break;
case 2:
displayItems();
break;
case 3:
displayRand();
break;
}
}
return 0;
}
void getNewItem() {
string strNewItem;
cin >> strNewItem;
vecItems.push_back(strNewItem);
}
void displayItems() {
for (int i = 0; i < vecItems.size(); i++) {
cout << vecItems.at(i) << endl;
}
}
void displayRand() {
int intRandIndex;
//random number generator
srand((unsigned)time(0));
intRandIndex = rand() % 10;
cout << vecItems.at(intRandIndex) << endl;
}
截图一
截图2
编辑:Visual Studio 版本是 Visual Studio 2015,我在全新的项目中重新编译和运行都无济于事。
最佳答案
我猜这个问题是 vs 2015 中默认的一些编译器警告设置阻止使用未初始化的变量,所以在使用它之前初始化 intInput:
int intInput = 0;
关于c++ - "unexpected tokens following preprocessor directive - expected a newline",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41471251/