【问题标题】:parse error before '' *2'' *2 之前的解析错误
【发布时间】:2015-08-18 22:35:48
【问题描述】:

这里是代码

#include <iostream>
#include <string>
#include "char.h"
#include "user.h"

bool user::readYN (string ans)
{
    ans = tolower (ans);
    if (ans == "y" || ans == "yes")
    {
        return true;
    }
    else if (ans == "n" || ans == "no")
    {
        return false;
    }
    else 
    {
        std::cout<<playr.inputErr;
        return false;
    }
}

字符串 ans 是从我的 main.cpp 传递给它的,应该是 y\n 以确认字符名称。但是,每当我尝试编译时,我都会收到错误

user.cpp:6: '{'之前的解析错误

和错误的人

user.cpp:8: '+' 之前的解析错误

我不知道它在哪里得到了 { 的错误,我不知道它在哪里看到 +(我的想法是平等的......也许???)我所有其他代码都已编译,我只是想要开始实际构建东西。

编辑:根据请求,头文件如下

用户.h

#ifndef user
#define user

#include<string>
class user
{
    public:
        string input;
        static string inputErr;
        bool readYN(string);
        void read(string);

}playr;

#endif

和 char.h

#ifndef chara
#define chara

#include<string>
class charecter
{
    public:
        string name;
        bool yes;
        int HP;
        void nameMe(string);
}chara;

#endif

【问题讨论】:

  • 尝试用 std::string 替换字符串
  • 您发布的代码中没有+。您确定您发布了正确的代码吗?
  • 是的,我复制粘贴了它。为什么我对它在那里感到困惑
  • 也许tolower 用作宏? AFAIK tolower 可以得到 char 而不是字符串。是的,你错过了namespace std
  • @ArtoriusIV #include "char.h" #include "user.h" 这些非标准头文件可能有问题。但不幸的是你没有透露:-( ...

标签: c++ compiler-errors


【解决方案1】:

改变

bool user::readYN (string ans)

bool user::readYN (std::string ans)

【讨论】:

  • 我这样做了,但编译错误没有改变。想解释一下为什么应该修复它?
  • @ArtoriusIV:由于您使用了std::cout(而不仅仅是cout),因此期望您也需要std::string 是完全合乎逻辑的。
【解决方案2】:

问题已解决。 user.h 中的定义与类声明有关。

【讨论】:

  • 最好使用#include 保护宏的命名约定,以确保它不会与类的名称冲突。例如USER_HUSER_HEADER.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多