2017-06-17 72 views
-5

所以我有这个工作,并且即将把它放在我的gitub上,当我意识到我忘了评论,所以我回去做了,我想我做错了,因为现在我我得到错误我不知道如何摆脱。我开始评论我的C++程序,然后得到错误

#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”。它纯粹是教育性的,并不意味着被自己使用,但可以用于更完整的程序。

+0

我在回答中提供了一个工作解决方案。以下是与工作代码的链接:http://ideone.com/fwbQVy。您需要将其与您的比较以查看差异。最好是摆脱所有评论来修复代码本身。您可以稍后添加评论。 – Azeem

+1

@Josh:你应该避免在代码中多余的评论。尽量让你的代码可读。像“OS =”Unix“这样的一行; //如果为真(成功),则将“Unix”分配给变量“SYS”。“不会给读取器带来任何东西,只会使代码混乱。这会让你的代码更难读。您的代码非常易读,不需要评论即可轻松完成。评论应限于解释算法和/或关于输入,错误条件等的特殊考虑的单个块...通常称为“合同”。大多数功能根本不需要评论,但是文档。 –

回答

1

有多个语法错误!

下面是你的代码片段(没有评论),它是类定义中的自由代码。它应该在一些功能上。你不能这样写代码。检查实时代码并与您的代码进行比较。

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

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

重要 遵循缩进和评论的一贯作风。

花一些时间去穿过C++ Coding Guidelines

+0

oooooooo.k。那没用。 IDE没有给出任何错误,但gcc只是抛出了整个buch编辑:继承人这不是ganna适合在这里的链接。 https://github.com/Myersj281/Small-Projects-Cpp/wiki – Josh

+0

@Josh:什么没有工作? – Azeem

+0

你在代码中究竟做了什么?请用你的问题陈述更新你的问题。 – Azeem

0

这不是一个解决方案本身,我只是想表明@Josh如何使用一个(不为不存在的)正确的编码风格,压痕,将使他的应用程序(与任何应用程序)非常可读,并且评论最少。

#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; 
} 
相关问题