我是 C++ 新手,我想知道如何正确定义 functions?
现在我收到以下错误:
Severity Code Description Project File Line Suppression State
Error (active) E0065 expected a ';' ConsoleMath D:\C++\ConsoleMath\ConsoleMath\ConsoleMath.cpp 18
Error C2601 'Basic': local function definitions are illegal ConsoleMath D:\C++\ConsoleMath\ConsoleMath\ConsoleMath.cpp 18
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
// Initialization
string command;
bool loop = true;
float number1 = 0, number2 = 0;
// Start-up
cout << "Welcome to Console Math v1.0.0\n\nThis software can be used for a variety of mathematical operations.\n\nFor list of commands; type \"help\"\nTo exit the program; type \"exit\"\n"
<< endl;
// Commands
void Basic()
{
cout << " >";
// Code here
}
// Main software loop
while (loop == true)
{
cout << "> ";
cin >> command;
cout << endl
<< endl;
if (command == "help")
{
cout << "Command layout (\">\" symbolizes new input after entering the previous one):\n> DatabaseName > operationName > detail name\n\n> basic\n > add\n > subtract\n > multiply\n > divide\n> advanced\n > potentiation\n > squareRoot\n > findPrimeNumbers\n > logarithm\n > factorial\n > function\n > intersection\n > linear-linear\n > linear-parabola\n > geometry\n > rectangle\n > perimeter\n > area\n > visualize\n > triangle\n > perimeter\n > area\n > basic\n > hemmonDefinedS\n > hemmonDefineS\n > coordinate\n > height\n > circle\n > area\n > circumference\n> trigonometry\n > sin\n > cos\n > tg\n > ctg\n> degrees\n > add\n > subtract\n > multiply\n > divide\n > other\n > random\n > randomRange";
}
else if (command == "basic")
{
Basic();
}
else if (command == "exit")
{
loop = false;
}
else
{
cout << "\"" << command << "\" is not a valid command. If you're having trouble please type \"help\"";
}
cout << endl
<< endl;
}
return 0;
}
有没有什么办法解决这一问题?
更新:这是我将 function 放在“main()”之外时的代码......有没有办法从“main()”调用它。
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
using namespace std;
void Basic()
{
cout << " >";
// Code here
}
int main()
{
// Initialization
string command;
bool loop = true;
float number1 = 0, number2 = 0;
// Start-up
cout << "Welcome to Console Math v1.0.0\n\nThis software can be used for a variety of mathematical operations.\n\nFor list of commands; type \"help\"\nTo exit the program; type \"exit\"\n"
<< endl;
// Commands
// Main software loop
while (loop == true)
{
cout << "> ";
cin >> command;
cout << endl
<< endl;
if (command == "help")
{
cout << "Command layout (\">\" symbolizes new input after entering the previous one):\n> DatabaseName > operationName > detail name\n\n> basic\n > add\n > subtract\n > multiply\n > divide\n> advanced\n > potentiation\n > squareRoot\n > findPrimeNumbers\n > logarithm\n > factorial\n > function\n > intersection\n > linear-linear\n > linear-parabola\n > geometry\n > rectangle\n > perimeter\n > area\n > visualize\n > triangle\n > perimeter\n > area\n > basic\n > hemmonDefinedS\n > hemmonDefineS\n > coordinate\n > height\n > circle\n > area\n > circumference\n> trigonometry\n > sin\n > cos\n > tg\n > ctg\n> degrees\n > add\n > subtract\n > multiply\n > divide\n > other\n > random\n > randomRange";
}
else if (command == "basic")
{
Basic();
}
else if (command == "exit")
{
loop = false;
}
else
{
cout << "\"" << command << "\" is not a valid command. If you're having trouble please type \"help\"";
}
cout << endl
<< endl;
}
return 0;
}
回答1
你试图在 function 中定义 function 在这种情况下 main
,你不能这样做。正确的方法如下
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
using namespace std;
// Commands
void Basic () {
cout << " >";
// Code here
}
int main () {
// Initialization
string command;
bool loop = true;
float number1 = 0, number2 = 0;
// Start-up
cout << "Welcome to Console Math v1.0.0\n\nThis software can be used for a variety of mathematical operations.\n\nFor list of commands; type \"help\"\nTo exit the program; type \"exit\"\n" << endl;
// Main software loop
while (loop == true) {
cout << "> ";
cin >> command;
cout << endl << endl;
if (command == "help") {
cout << "Command layout (\">\" symbolizes new input after entering the previous one):\n> DatabaseName > operationName > detail name\n\n> basic\n > add\n > subtract\n > multiply\n > divide\n> advanced\n > potentiation\n > squareRoot\n > findPrimeNumbers\n > logarithm\n > factorial\n > function\n > intersection\n > linear-linear\n > linear-parabola\n > geometry\n > rectangle\n > perimeter\n > area\n > visualize\n > triangle\n > perimeter\n > area\n > basic\n > hemmonDefinedS\n > hemmonDefineS\n > coordinate\n > height\n > circle\n > area\n > circumference\n> trigonometry\n > sin\n > cos\n > tg\n > ctg\n> degrees\n > add\n > subtract\n > multiply\n > divide\n > other\n > random\n > randomRange";
} else if (command == "basic") {
Basic ();
} else if (command == "exit") {
loop = false;
} else {
cout << "\"" << command << "\" is not a valid command. If you're having trouble please type \"help\"";
}
cout << endl << endl;
}
return 0;
}