1

This is my header file and my CPP file uses the arguments from the main function to initialize these variables with values, But even this is giving me linking errors. How do I solve this issue? The error codes are LNK1120 - unresolved externals and LNK2001 - unresolved external symbol "double g_dailydiscount".

#ifndef FOODORDER_H
#define FOODORDER_H
#include <iostream>
    
extern double  g_dailydiscount;

extern double g_taxrate;

namespace abc {

    class FoodOrder {
        private:
            char m_name[10]{};
            char m_desc[25]{};
            double m_price;
            bool m_isDailySpecial;
        public:
            FoodOrder();
            void read(std::istream& is = std::cin);
            void display(std::ostream& os = std::cout);
    };
}

#endif // !FOODORDER_H

I tried to declare global variables in header file using extern keyword, But this is still giving me linking errors

Krins
  • 11
  • 3
  • You might be under the impression that every linker and every C++ compiler in the world uses the same exact error codes. This is not true. P.S. It is also not true that the `extern` keyword's purpose is to avoid linker errors. Of course, using or not using it, improperly, will result in linker error, but that's not its purpose. In addition to using the `extern` keyword to declare objects in global scope it also necessary to define them in exactly one translation unit, otherwise there will still be linker errors despite the fact that `extern` was used. Did you do that? – Sam Varshavchik Jan 17 '23 at 22:08
  • 1
    It would be better if you additionally posted the linker error messages as text into the question, instead of only posting an image. You may want to read this: [Why not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/q/285551/12149471) – Andreas Wenzel Jan 17 '23 at 22:10
  • Recommendation: don't re-ask questions. Beat the existing question into better shape instead. The server uses the quality of your questions, as assessed by the score given to the question by the community, to decide when you can ask your next question. Poorly received questions, score zero or less, stretch out the cooldown timer, eventually reaching one question every six months (AKA Question Ban). With a history of well-received questions (score 1 or better) you get a shorter cooldown and more site perks. Once you get "banned" it is damn hard to dig out and deleting questions will not help. – user4581301 Jan 18 '23 at 00:06
  • If you do not understand the duplicates edit the question and ask for clarification of the points confusing you. – user4581301 Jan 18 '23 at 00:07

0 Answers0