#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0

    Need Help Please


    I am a newbie in C++ programming and I don't know why this program failed to compile.

    Code:
    #include <iostream>
    #include "stdafx.h"
    
    class PoweredDevice
    {
    public:
    	PoweredDevice (int nPower)
    	{
    		using namespace std;
    		cout << "PoweredDevice: " << nPower << endl;
    	}
    };
    
    class Scanner: virtual public PoweredDevice
    {
    public:
    	Scanner (int nScanner, int nPower)
    		: PoweredDevice(nPower)
    	{
    		using namespace std;
    		cout << "Scanner: " << nScanner << endl;
    	}
    };
    
    class Printer: virtual public PoweredDevice
    {
    public:
    	Printer ( int nPrinter, int nPower)
    		: PoweredDevice(nPower)
    	{
    		using namespace std;
    		cout << "Printer: " << nPrinter << endl;
    	}
    };
    
    class Copier: public Scanner, public Printer
    {
    public:
    	Copier (int nScanner, int nPrinter, int nPower)
    		: Scanner (nScanner, nPower), Printer (nPrinter,nPower), PoweredDevice(nPower)
    	{
    	}
    };
    
    
    int main()
    {
    	Copier cCopier (1, 2, 3);
    }
    It shows error saying that 'cout' and 'endl' is undeclared identifier. Even though I tried to change all the cout and endl to std:cout and std:endl but it still doesn't work.
    Anyone please help me to solve this problem. Thank you.
    P.S: I am using Visual Studio 2012
    Here is the image for it:
    http://http://i.imgur.com/if9efDZ.png?1
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    I don't know why the code doesn't compile for you, it compiles for me with only the following warnings:

    main.cpp|17|warning: unused parameter ‘nScanner’ [-Wunused-parameter]|
    main.cpp|28|warning: unused parameter ‘nPrinter’ [-Wunused-parameter]|
    main.cpp||In constructor ‘Copier::Copier(int, int, int)’:|
    main.cpp|40|warning: base ‘Printer’ will be initialized after [-Wreorder]|
    main.cpp|40|warning: base ‘PoweredDevice’ [-Wreorder]|
    main.cpp|39|warning: when initialized here [-Wreorder]|
    For the last three warnings the initialization lists should match the order to the variables in the class declaration.

    Jim
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0
    Problem solved when I #include "stdafx.h" first followed by #include <iostream>. But I still don't understand why it works that way. Anyone can explain?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    I can't really tell you why since I don't use that header on my system. But that header is part of the pre-compile system and should always be included first.

    Jim

IMN logo majestic logo threadwatch logo seochat tools logo