【发布时间】: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