0

So I'm trying to convert my code into 3 files, the main.cpp, a header, and the classbody.cpp. This was my original code:

#include <iostream>

using namespace std;

#include <string>

#include <ctime>

#include <stdio.h>      

#include <stdlib.h>     

class colorPicker {

private:

    string colorArray[7];

public:

    colorPicker() {

        colorArray[0] = "Red";

        colorArray[1] = "Green";

        colorArray[2] = "Purple";

        colorArray[3] = "Yellow";

        colorArray[4] = "Orange";

        colorArray[5] = "Indigo";

        colorArray[6] = "Pink";

    }

    void printAllColors() {

        for (int i = 0; i < 7; i++)

        {
            cout << colorArray[i] << endl;
        }

    }

    string randomColor() {

        srand((unsigned)time(0));

        int i = 0;

        i = rand() % 7;

        return colorArray[i];

    }

};

int main()

{

    colorPicker P;

    P.printAllColors();

    cout << "Random Color: " << P.randomColor();

    system("pause");

    return 0;

}

If ran it'll list the colors in random order, now I'm trying to practice making header files. and these are my code:

Main.cpp

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <string>

#include <ctime>
#include <stdio.h>      
#include <stdlib.h>     
#include "ColorPicker.h"

int main()

{

    ColorPicker P;

    P.printAllColors();

    cout << "Random Color: " << P.randomColor();

    system("pause");

    return 0;

}

ColorPicker.h:

#pragma once
#include "stdafx.h"
#include <iostream>
using namespace std;
#ifndef "COLORPICKER_H"
#endif "COLORPICKER_H"

class colorPicker {

private:

public:
    ColorPicker();

};

And Finally, ColorPicker.cpp

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <string>

#include <ctime>
#include <stdio.h>      
#include "ColorPicker.h"

ColorPicker::ColorPicker() {
    class colorPicker {

    private:

        string colorArray[7];

    public:

        colorPicker() {

            colorArray[0] = "Red";

            colorArray[1] = "Green";

            colorArray[2] = "Purple";

            colorArray[3] = "Yellow";

            colorArray[4] = "Orange";

            colorArray[5] = "Indigo";

            colorArray[6] = "Pink";

        }

        void printAllColors() {

            for (int i = 0; i < 7; i++)

            {
                cout << colorArray[i] << endl;
            }

        }

        string randomColor() {

            srand((unsigned)time(0));

            int i = 0;

            i = rand() % 7;

            return colorArray[i];

        }

    };
};

I've decided to do it in this fashion because I watched "Buckys C++ Programming tutorials" and this was on his Header episode. However, my version is not valid, what am I doing wrong? and thank you in advanced!

2
Contributors
2
Replies
15
Views
3 Weeks
Discussion Span
Last Post by rubberman
0

UPDATE: I'm so smart! re-looking through my code I realized I put a whole class in the second CPP which defeats the whole purpose of the body function. after fixing that the code did not work, however doing a quick visual scan I noticed there was an Uppercase instead of a lowercase in the main file! Welp, now it's fixed!

0

In the colorPicker.cpp file you have specified the class colorPicker to be derived from itself. EitherRmove the ':ColorPicker' from the class declaration, or change the caps 'C' to lowercase 'c'. IE::class ColorPicker::colorpicker. That said, DON'T DO THAT! It will just come back and bite you on the rear.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.