Having Trouble with Compiling This?

I'm not sure what's wrong exactly. I keep getting different errors with different compilers and nothing will build this thing. Need a second opinion, thanks.


Last edited on
Please edit your post and make sure your code is [code]between code tags[/code] so that it has syntax highlighting and line numbers, as well as proper indentation.

You use a function getEmployeeList which is not defined in your code.

EDIT: I refuse to participate further in this thread: https://i.imgur.com/jt9vJg3.png
Last edited on
Syntax and indentation is right. getEmployeeList is a function I just didn't include that in this post. Just erase that. So your post is invalid please come back with an answer.
Syntax and indentation is right.

1. That is hard to see. You really should add those code tags, because it makes the code much easier to read.

2. You have implied that syntax is not right. It is the wrong syntax that generates compiler errors.

The compiler errors are very useful. They tell what/where is wrong. We cannot possibly help you (yet) to understand those messages, because you haven't told us the errors.


Variable length arrays are not a standard C++ feature.
[

I've tried updating xCode and everything, still doesn't work.

The error with VisualStudio is something completely different referring to
fstream people(name,ios::app); It's a MinGW error? I completely updated MinGW and still doesn't work.

I saw many examples where you could throw a int variable into that structure variable to make it an array of structures. As long as the value is there as the array is being initialized. I'm not sure if you can tell what I'm doing with this program, but I'd like an individual variable for each different structure that the user wants. However, I can throw a number into the statement like so:
and that seems to compile fine. I just don't want to have 100 structures if the user only asks for 3. Opposite, I don't want to only have 100 structures if the user asks for 150..
Last edited on

1
2
cin >> Number;
Employee emp[Number];


You cant do this in c++. The size of the array has to be a constant. If you want to do this, you'd have to allocate memory for it.

cin >> Number;
Employee* emp = new Employee[Number];

That'll fix your problem. If this is not a school assignment and you're allowed to use vectors, then I'd recommend that.
Last edited on
@TarikNeaj Thank-You. Could you explain this to me? I've never seen the
new int[x];
Would I still access the individual structures like so:


?
Last edited on
I tried it and got this new error.
26:15: error: cannot initialize a variable of type 'Employee *' with
an rvalue of type 'int *'
Employee* emp = new int[Number];
Would I still access the individual structures like so:


You would have never access it this way ever :D What you're doing there is creaing new arrays. You just want to loop through one. So just do

1
2
3
4
for(int i=0;i<number;i++)
{
     emp[i];
}


Could you explain this to me? I've never seen the
new int[x];


You can read here - http://www.cplusplus.com/doc/tutorial/dynamic/
And watch this -
https://www.youtube.com/watch?v=C_hYF7YG_yQ&index=24&list=PL2DD6A625AD033D36&ab_channel=CodingMadeEasy

If you still dont get it, I'll explain.

I tried it and got this new error.


Oh Im sorry, I gave you the wrong thing!
Its suppose to look like this Employee* emp = new Employee[Numbers];

Just to give you some quick info. The only thing this does is just allocate memory on the heap, rather than stack. Which is something you can look up anywhere :)
Please, no new like that. The manual dynamic allocation of memory with new is standard C++, but modern C++ has easier and safer alternatives. The thing is that if you explicitly allocate memory, then you have to deallocate it appropriately too and that is anything but trivial.

There are still situations, where you will have to use new (and raw pointers) but by the time you will have to face them, your overall C++ knowledge will be better.

With C++14 the line would be:
auto emp = std::make_unique<Employee[]>( Numbers );
See: http://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique

The object that is created there (emp) behaves like plain array, but does ensure that the array is automatically deallocated at proper moment.


However, there is an alternative that has much more features; the std::vector.
std::vector<Employee> emp( Numbers );
The emp is still used like an array:
1
2
3
4
for( size_t i = 0; i < emp.size(); ++i )
{
  std::cout << emp[i];
}


Passing to a function naturally changes:
1
2
void saveEmployee( std::fstream & p, std::vector<Employee> & e );
void saveEmployee( std::fstream & p, const std::unique_ptr<Employee[]> & e );
Last edited on
Sure @Keskiverto. But I naturally assume people who post this kind of things are studying and this is a homework/assignment. And in those you cant use whatever you want, and they probably want them to use new and delete so they get an idea of what it is.

But if you're doing this on your own @OP. Go with what the post above me showed.
Thank You Guys that helped a lot! I've finished it completely. The whole
Employee* emp = new Employee[Numbers]; part had never been explained to me. That opens quite a few windows
Topic archived. No new replies allowed.