+ 2
Please tell the meaning of error and how to resolve it as i cant find anything online....
#include <iostream> using namespace std; class system { float amount; public: system(float a): amount(a) { } if (amount >= 1000) { amount = amount-(amount*0.1); } cout << "The net amount to be paid is:" << amount; }; The error is "excepted member name or ';' after declaration specifiers."
4 odpowiedzi
+ 2
Rashim Narayan Tiku You are trying to execute statements that don't belong to any particular method in the declaration of a class, which is illegal C++.To put it simple, it's just the syntax of C++ not allowing you to do so.
Sorry I omitted the details in the previous post. Your full code should include both the if and cout statements in the constructor body.
+ 5
The braces after the constructor are in the wrong place. The if statement should be in the constructor body, not after.
Expected:
system (float a) : amount (a) {
if (...)
}
Error:
system (float a) : amount (a) {}
if (...)
+ 1
Hoàng Nguyễn Văn Thanks
but why is this way wrong ?
P.S. the cout after the if(...) also needs to be inside the constructor to remove the error...
0
Rashim Narayan Tiku Classes are data types, like structures, not subprograms, like functions, even though they contain code.