我开始评论我的C ++程序然后出错了

时间:2017-06-17 07:45:57

标签: compiler-errors c++14

所以我有这个工作,并且当我意识到我忘了评论时就要把它放在我的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”。它纯粹是教育性的,并不意味着单独使用,但可以在更完整的程序中使用。

2 个答案:

答案 0 :(得分:1)

有多种语法错误!

这是您的代码片段(没有注释),它是类定义中的免费代码。它应该是某种功能。你不能写那样的代码。检查实时代码并与您的代码进行比较。

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

以下是代码的功能版:http://ideone.com/fwbQVy

重要 遵循一致的缩进和评论样式。

花一些时间浏览C++ Coding Guidelines

答案 1 :(得分:0)

这本身不是解决方案,我只是想向@Josh展示如何使用 a (不是 ,因为它不存在)正确的编码风格,缩进,会使他的应用程序(和任何应用程序)非常易读,只需要最少的评论。

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