Hi I am new to C++ and testing a structure in a c++ code in dev-c++. but it is not compiling and giving errors . It is working well with .h extensions in pre-processor directives in dev-c++. so i Dont think so it carries .h extension problems
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
struct car
{
const int MAX=10;
char model[MAX];
char spare_part[MAX];
float cost;
}
int main()
{
car BMW ;
BMW.model[MAX]="SLR-8 S";
BMW.spare_part[MAX]="SILENCER";
BMW.cost=175.56F;
cout << setw(50) << "\n\n WELCOME TO SHOWROOM" << endl << endl;
cout << "CAR MODEL: " << BMW.model[MAX] << endl;
cout << "SPARE PART: " << BMW.spare_part[MAX] << endl;
cout << "COST OF PRODUCT: " << BMW.cost[MAX] << endl;
return 0;
}
Compiler logs are:
Compiler: Default compiler
Executing g++.exe...
g++.exe "D:\cdev\projects\structure.cpp" -o "D:\cdev\projects\structure.exe" -I"D:\cdev\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"D:\cdev\Dev-Cpp\include\c++\3.4.2\backward" -I"D:\cdev\Dev-Cpp\include\c++\3.4.2\mingw32" -I"D:\cdev\Dev-Cpp\include\c++\3.4.2" -I"D:\cdev\Dev-Cpp\include" -L"D:\cdev\Dev-Cpp\lib"
In file included from D:/cdev/Dev-Cpp/include/c++/3.4.2/backward/iostream.h:31,
from D:\cdev\projects\structure.cpp:1:
D:/cdev/Dev-Cpp/include/c++/3.4.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
D:\cdev\projects\structure.cpp:6: error: ISO C++ forbids initialization of member `MAX'
D:\cdev\projects\structure.cpp:6: error: making `MAX' static
D:\cdev\projects\structure.cpp:13: error: new types may not be defined in a return type
D:\cdev\projects\structure.cpp:13: error: extraneous `int' ignored
D:\cdev\projects\structure.cpp:13: error: `main' must return `int'
D:\cdev\projects\structure.cpp: In function `int main(...)':
D:\cdev\projects\structure.cpp:16: error: `MAX' undeclared (first use this function)
D:\cdev\projects\structure.cpp:16: error: (Each undeclared identifier is reported only
once for each function it appears in.)
Execution terminated
Values in stuct should be initialized in a constructor, or should be static. Like:
struct car
{
static const int MAX=10;
...
There is a semicolon missing after the struct definition. It should be like:
struct car
{
static const int MAX=10;
char model[MAX];
char spare_part[MAX];
float cost;
};
Check your semicolons. One is required after the closing } of the struct.
Also, declare the MAX variable static
, ie
{
static const int MAX=10;
but the compiler tells you that one quite nicely...
And the third error comes from the fact that you declare MAX
inside car
, you'd have to reference it accordingly as car::MAX
. But actually, the whole BMW.model[MAX]="SLR-8 S";
statement doesn't really make sense. That would mean 'assign the string "SLR-8 S" to the character after the last one in BMW.model' (for a char[MAX], the valid indices start at 0, and go to (MAX-1)!). Best use std::string
instead of char model[MAX]
, that's by far easier to handle! Eg:
struct car
{
std::string model;
Then you can simply say
BMW.model="SLR-8 S";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.