所以我有这个工作,并且即将把它放在我的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”。它纯粹是教育性的,并不意味着被自己使用,但可以用于更完整的程序。
我在回答中提供了一个工作解决方案。以下是与工作代码的链接:http://ideone.com/fwbQVy。您需要将其与您的比较以查看差异。最好是摆脱所有评论来修复代码本身。您可以稍后添加评论。 – Azeem
@Josh:你应该避免在代码中多余的评论。尽量让你的代码可读。像“OS =”Unix“这样的一行; //如果为真(成功),则将“Unix”分配给变量“SYS”。“不会给读取器带来任何东西,只会使代码混乱。这会让你的代码更难读。您的代码非常易读,不需要评论即可轻松完成。评论应限于解释算法和/或关于输入,错误条件等的特殊考虑的单个块...通常称为“合同”。大多数功能根本不需要评论,但是文档。 –