1

Following an example at: http://www.learncpp.com/cpp-tutorial/47-structs/ relating to structs, and when I tried to compile this program:

#include <iostream>
void PrintInformation(Employee sEmployee)
{
std::cout<<"ID: "<<sEmployee.nID<<std::endl;
std::cout<<"Age: "<<sEmployee.nAge<<std::endl;
std::cout<<"Wage: "<<sEmployee.fWage<<std::endl;
}

struct Employee {int nID;int nAge;float fWage;};

int main()
{
Employee abc;
abc.nID=123;
abc.nAge=27;
abc.fWage=400;
// print abc's information
PrintInformation(abc);
return 0;
}

I get the following:

alt text

Why is that?

Thanks.

Simplicity
  • 47,404
  • 98
  • 256
  • 385
  • see this topic [Why do functions need to be declared before they are used?](http://stackoverflow.com/questions/4757705/why-do-functions-need-to-be-declared-before-they-are-used) – Nawaz Jan 22 '11 at 11:01
  • @Nawaz. Thanks for directing me to the page. – Simplicity Jan 22 '11 at 11:26

1 Answers1

9

You need to declare the struct before the function that attempts to use it.

C (and by extension, C++) were designed for "single-pass" compilation. Therefore, everything must be available to the compiler by the time it's required.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680