【问题标题】:I started commenting my C++ program then got errors [closed]我开始评论我的 C++ 程序然后出现错误 [关闭]
【发布时间】:2017-11-19 23:44:37
【问题描述】:

所以当我意识到我忘记发表评论时,我有这个工作并且正准备将它放在我的 gitub 上,所以我回去做了,我想我做错了什么,因为现在我遇到了我不知道的错误如何摆脱。

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

class ENVIROMENT //Defines a class.
   {
   public:
   string OS; //Difines a string.
          {
          if (char const* USER = std::getenv("USER")) //Checks for user      name on a Unix-like system.
          {
          OS = "Unix"; //If true (succeded), assigns "Unix" to the variable "SYS".
          }
          else if(char const* USER = std::getenv("USERNAME")) //If last check was false (failed), checks for the username on Windows.
             {
             OS = "Windows"; //If true (succeded), assigns "Windows" to the variable "SYS".
             }
             else
             {
             OS << "Your system is not supported!"; //If both returned false (failed), assignes a "System not supported" message to SYS.
             }
          cout << OS << endl; //Tells the user what system they have or that it is not supported (meaning it doesn't know what OS it is).
          return OS; //Returns OS to the string "OS".
          }
   };

int main()
   {
   ENVIROMENT CHECK; //Calls the class "ENVIROMENT", refers to it as "CHECK".
   CHECK.OS(); //Calls the function (string) "OS" from the class "ENVIROMENT".
   return 0; //Returns a value of 0 if int "main" executed successfuly.
   }

错误:

ERROR: 10:11 Expected member name or ';' after declaration specifiers.

ERROR: 31:4 Type string does not provide a call operator.

我的问题是:这些错误是什么意思?我知道我一定遗漏了一些非常基本的东西。

编辑:它应该根据用户名系统变量检测正在运行的操作系统。在类 unix 系统上返回“Unix”,在 Windows 上返回“Windows”。它纯粹是教育性的,不能单独使用,但可以在更完整的程序中使用。

【问题讨论】:

  • 我在回答中提供了一个可行的解决方案。这是工作代码的链接:ideone.com/fwbQVy。您需要将其与您的进行比较才能看到差异。最好去掉所有的 cmets 来修复代码本身。您可以稍后添加 cmets。
  • @Josh:您应该避免代码中出现多余的 cmets。尝试使您的代码可读。像OS = "Unix"; //If true (succeded), assigns "Unix" to the variable "SYS". 这样的行不会给读者带来任何东西,只会使代码混乱。这会使您的代码的可读性降低很多。没有 cmets,您的代码非常易读且易于理解。评论应仅限于解释算法和/或关于输入、错误条件等的特殊考虑的单个块......通常称为“合同”。大多数函数根本不需要 cmets,而是需要文档。

标签: compiler-errors c++14


【解决方案1】:

有多个语法错误!

这是您的代码的 sn-p(没有 cmets),它是类定义中的免费代码。它应该在某些功能中。你不能写这样的代码。检查实时代码并将其与您的代码进行比较。

// ...
public:
   string OS;
   {
       if (char const* USER = std::getenv("USER"))
   {
// ...

这是您的代码的功能版本:http://ideone.com/fwbQVy

重要 对缩进和 cmets 遵循一致的样式。

花点时间浏览一下C++ Coding Guidelines

【讨论】:

  • oooooooo.k。那没有用。 IDE 没有给出任何错误,但 gcc 只是抛出了整个 buch 编辑:这是一个不适合这里的链接。 github.com/Myersj281/Small-Projects-Cpp/wiki
  • @Josh:什么没用?
  • 你到底想在你的代码中做什么?请用您的问题陈述更新您的问题。
  • 好吧,没关系,终端内置的 IDE 编译得很好,但 sakura 没有
  • 你是怎么编译的?命令?
【解决方案2】:

这本身不是一个解决方案,我只是想向@Josh 展示如何使用 a(不是 the,因为它不存在)正确的编码风格,缩进, 将使他的应用程序(以及任何应用程序)非常易读,并且使用最少的 cmets。

#include <iostream>
#include <cstdlib>
#include <string>

// This application tries to deduce the OS by probing from environment strings. 

class Environment
{
public:
  static std::string getOS() 
  {
    // tries to get the OS by getting the username from the environment.
    // returns the name of the OS, or an empty string on error.

    if (std::getenv("USER"))
    {
      return "Unix";
    }
    else if (std::getenv("USERNAME"))
    {
      return "Windows";
    }
    return "";
  }
};

int main()
{
  std::string osName = Environment::getOS();

  if (osName.empty())
  {
    std::cout << "Your system is not supported!\n"; 
    return 1;
  }

  std::cout << "Running under: " << osName << '\n';

  return 0;
}

【讨论】:

    猜你喜欢
    • 2013-12-21
    • 2016-03-01
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多