does not name a type

I have this error that i don't know how to get rid of. Some help please. Thanks in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  
struct car

{
	
string marka;

string model;

int obujam;

int masa;

int max_snaga_kw;

int max_snaga_ks;

int max_brzina;

float ubrzanje;

};

vector<car>allCars(56); 

allCars[0] = { "AUDI", "R8 SPYDER", 5204, 1720, 386, 535, 313, 4.1 };


it says allCars does not name a type in allCars[0] =...
You need to put that code inside a function.
There are only a few things you can do in code at global scope (i.e. not inside a function/method definition). You can declare an object, as you do with allCars, and as part of that declaration you can initialise it.

But you can't have a separate assignment statement. That needs to go into a function or method.
Can you please give me an examle. I am kinda new to this so...
Like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstdio>

using namespace std;

struct car
{
  string marka;
  string model;
  int obujam;
  int masa;
  int max_snaga_kw;
  int max_snaga_ks;
  int max_brzina;
  double ubrzanje;
};

int main()
{
  vector<car>allCars(56);

  allCars[0] = { "AUDI", "R8 SPYDER", 5204, 1720, 386, 535, 313, 4.1 };
}


BTW: It's normally better to use double instead of float since they have precision.
thanks for the help. i don't need double since i have only one decimal and i know exactly what the value is.
i know exactly what the value is

Are you sure? The decimal value 4.1 can not be stored exactly as a floating-point value so what you get is an approximation.

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <iomanip>

int main()
{
	std::cout << std::setprecision(100);
	std::cout << "4.1 as a float:  " << 4.1f << "\n";
	std::cout << "4.1 as a double: " << 4.1  << "\n";
}
4.1 as a float:  4.099999904632568359375
4.1 as a double: 4.0999999999999996447286321199499070644378662109375
Last edited on
While what you say is true, if the difference between float and double actually makes a significant difference, the problem is using floating-point representation in the first place and not the lack of extra precision that double gives (unless you're doing some high-precision scientific computing or something). For OP's purposes for ubrzanje (acceleration), I really doubt it makes a difference as to whether the number is 4.09999990 or 4.09999999 -- the more important fact is that it does not equal 4.1 (exactly), as Peter87 points out.

It's always good to know that there is a difference in precision... it's just that the more important factor to communicate is knowing that floating-point representation is fundamentally inaccurate, regardless of the exact precision.

And a bit unrelated, but things like money should never be represented as floating point for any serious application either, for this same reason (whether internally a 32-bit floating point or a 64-bit floating point).

Edit: Just to clarify, one example where it certainly can make a difference is when you have error propagation through repeated iterations. This could make one small error snowball into a bigger error. But then the difference between float and double just becomes a matter of the number of iterations until the chaos becomes unmanageable. So sure, prefer double in general (pre-optimization is the root of all evil), but the fundamental problem is still the floating point, and this should not be understated (i.e using double instead of float won't magically fix anything "in the general case").
Last edited on
I have solved my problem. Thank you all!
Topic archived. No new replies allowed.