0

I am having difficulty executing a program that I wrote with structs. My program has a header file, an initialization file, and a main file. When I compiled it, the compiler complained and later I found out that I forward declared my struct name as Company but initialized it using company so I changed it to Company but still the compiler complains after I did this. How can I fix this? Any help will be greatly appreciated. Below is my code for my three files:

structs.h:

#ifndef STRUCTS_H
#define STRUCTS_H

struct Company{
  double salary;
  int workers;
  int bosses;
}

#endif

initialization.cpp:

Company a = {1200340.99, 30000, 3};
Company b = {500320.85, 5000, 2};

main.cpp:

#include <iostream>
#include "structs.h"

void PrintInfo(Company company){
  using namespace std;
  cout << "salary: " << Company.salary << endl;
  cout << "workers: " << Company.workers << endl;
  cout << "bosses: " << Company.bosses << endl;
}

int main(){
  PrintInfo(a);
  PrintInfo(b);
  return 0;
}
3
  • Seems like you have missed the ';' at the struct :-) Commented Dec 6, 2013 at 13:37
  • 1
    Note that 'forward declared' means something different. There's no forward declaration in this code just regular declaration. Commented Dec 6, 2013 at 13:38
  • please make sure you show the exact error messages when writing your questions. Commented Dec 6, 2013 at 13:51

3 Answers 3

4
  1. You need a ; after the definition of struct Company

  2. In PrintInfo you need to reference the object company (lowercase c) and not the class Company (uppercase C), e.g.

    cout << "salary: " << company.salary << endl;   // lowercase c
    cout << "workers: " << company.workers << endl; // lowercase c
    cout << "bosses: " << company.bosses << endl;   // lowercase c
    
  3. As a and b is initialized (globally) in a different source file you must redeclare them with external linkage in the source file that need to access them using the extern keyword, e.g.

    // main.cpp
    #include <iostream>
    #include "structs.h"
    
    extern Company a;
    extern Company b;
    
    /* ... */
    

    Consider initializing a and b where they are used instead:

    int main() {
        Company a = {1200340.99, 30000, 3}; // Init here.
        Company b = {500320.85, 5000, 2};   // Init here.
    
        PrintInfo(a);
        PrintInfo(b);
        // return 0; // Unnecessary in main function.
    }
    
  4. In function PrintInfo, as you are not modifying the argument you should pass the class Company as reference to const to avoid copying, i.e. declare the function using this:

    void PrintInfo(const Company& company)
    
Sign up to request clarification or add additional context in comments.

1 Comment

Also some extern is missing for a and b in the OP's sample.
2

A structure requires a ';' at the end

struct Company{
  double salary;
  int workers;
  int bosses;
};

Comments

0

In addition to the other answers, the code in main.cpp knows nothing about a and b in your initialization.cpp.

You need to either add an extern declaration in structs.h or move those into main.cpp.

But, you should also consider not making them global variables, like this:

int main()
{
    Company a = {1200340.99, 30000, 3};
    Company b = {500320.85, 5000, 2};
    PrintInfo(a);
    PrintInfo(b);
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.