【问题标题】:"unexpected tokens following preprocessor directive - expected a newline"“预处理器指令后的意外标记 - 需要换行符”
【发布时间】:2017-01-04 18:54:06
【问题描述】:

我创建了这个程序,出于某种原因,Visual Studio 给出了这个“警告”(“预处理器指令后的意外标记 - 需要一个换行符”),同时还发现错误似乎引用了该行下一行的内容编译器声称他们在。瞬间,错误Carat Error 似乎指的是仅适用于one line down 的东西。因此,我相信程序的标题中一定有什么可怕的错误,但我不确定。这是代码:

    //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;
}

截图 1

截图 2

编辑:Visual Studio 版本是 Visual Studio 2015,我重新编译并在全新项目中运行都无济于事。

【问题讨论】:

  • 给出的示例不会在 Visual Studio 2015 或 Visual Studio 2010 中重现此错误。您使用的是哪个版本的 Visual Studio?你确定这是你要给出的例子吗?
  • 我正在使用 Visual Studio 15,并重新编译了程序并从头开始重新制作,结果却遇到了同样的问题:/
  • 也许问题在于您的预编译头文件。我必须排除 #include "stdafx.h" 行才能编译(我没有这个头文件)。
  • 我的编译器需要它,除非它存在,否则不会运行。它也从来没有给我带来过问题
  • 尝试删除导致错误的行以及上一行和下一行,然后重新输入(不要复制/粘贴)。我曾经遇到过一个问题,即不可显示的字符导致编译器错误。

标签: c++ token newline


【解决方案1】:

我猜的问题,如果默认情况下 vs 2015 中的某些编译器警告设置阻止使用未初始化的变量,因此在使用之前初始化 intInput:

int intInput = 0;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 2010-09-19
    相关资源
    最近更新 更多