+ 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."

24th Oct 2018, 4:22 AM
Rashim Narayan Tiku
Rashim Narayan Tiku - avatar
4 ответов
+ 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.
24th Oct 2018, 6:28 AM
Hoàng Nguyễn Văn
Hoàng Nguyễn Văn - avatar
+ 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 (...)
24th Oct 2018, 4:38 AM
Hoàng Nguyễn Văn
Hoàng Nguyễn Văn - avatar
+ 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...
24th Oct 2018, 5:18 AM
Rashim Narayan Tiku
Rashim Narayan Tiku - avatar
0
Rashim Narayan Tiku Classes are data types, like structures, not subprograms, like functions, even though they contain code.
25th Oct 2018, 1:18 PM
Vlad Serbu
Vlad Serbu - avatar