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!

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!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.