Chalandria
Chalandria

Reputation: 3

int and vector "was not declared in this scope"

This is my first post in SO. I am having trouble getting this to compile. It keeps saying that journalKey and journalKeyCount are not declared in this scope. It also says that my constructor, destructor, and all my functions are "redefinitions".

this is the .h

/** class journal
    Project 1.
    @file journal.h */

#ifndef JOURNAL_H
#define JOURNAL_H
#include <vector>

class journal
{
    private:
        char journalName;

    public:
        journal();
        virtual ~journal();

        int journalKeyCount = 0;
        vector<char> journalKey;

        void makeJournal(const char journalName); //Adds a journal to the journalKey vector
        void displayJournal(const int journalID); //Outputs the data of a journal
};

#include "journal.cpp"
#endif // JOURNAL_H

and this is the .cpp

/** class journal
    Project 1.
    @file journal.cpp */

using namespace std;
#include <iostream>
#include "journal.h"
#include <vector>

/**<
Default constructor.
*/
journal::journal()
{
}

/**<
Default deconstructor.
*/
journal::~journal()
{
}

/**<
Adds a journal to the journalKey vector.
@retuen void
@param journalName the name of the journal being added.
@pre -
@post The new journal name has been added to the end of the journalKey vector
*/
void makeJournal(const char journalName)
{
    journalKey.push_back(journalName);
    //journalKey[journalKeyCount] = journalName;
    journalKeyCount++;
}

/**<
Outputs the name of a journal that is set to the provided key.
@retuen void
@param journalID they ID key of the journal name.
@pre journalID key must exist.
@post -
*/
void displayJournal(const int journalID)
{
    if(journalID > journalKeyCount)
        cout << "Journal does not exist" << endl;
    else
        cout << "Journal Name: " << journalKey[journalID] << endl;
}

Thank you in advance for and advice.

Updated

.h

/** class journal
    Project 1.
    @file journal.h */

#ifndef JOURNAL_H
#define JOURNAL_H
#include <vector>

class journal
{
    private:
        char journalName;

    public:
        journal();
        virtual ~journal();

        int journalKeyCount = 0;
        vector<char> journalKey;

        void makeJournal(const char journalName); //Adds a journal to the journalKey vector
        void displayJournal(const int journalID); //Outputs the data of a journal
};

#endif // JOURNAL_H

.cpp

/**<
Default deconstructor.
*/
journal::~journal()
{
}

/**<
Adds a journal to the journalKey vector.
@retuen void
@param journalName the name of the journal being added.
@pre -
@post The new journal name has been added to the end of the journalKey vector
*/
void journal :: makeJournal(const char journalName)
{
    journalKey.push_back(journalName);
    //journalKey[journalKeyCount] = journalName;
    journalKeyCount++;
}

/**<
Outputs the name of a journal that is set to the provided key.
@retuen void
@param journalID they ID key of the journal name.
@pre journalID key must exist.
@post -
*/
void journal :: displayJournal(const int journalID)
{
    if(journalID > journalKeyCount)
        cout << "Journal does not exist" << endl;
    else
        cout << "Journal Name: " << journalKey[journalID] << endl;
}

error log is much shorter but im still getting:

warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]|
error: redefinition of 'journal::journal()'|
error: 'journal::journal()' previously defined here|
error: redefinition of 'journal::~journal()'|
error: 'virtual journal::~journal()' previously defined here|
error: redefinition of 'void journal::makeJournal(char)'|
error: 'void journal::makeJournal(char)' previously defined here|
error: redefinition of 'void journal::displayJournal(int)'|
error: 'void journal::displayJournal(int)' previously defined here|

You guys are fast and so helpful =D

Upvotes: 0

Views: 5652

Answers (1)

David G
David G

Reputation: 96810

When defining member functions outside their class you need to use a nested name specifier, or journal:: to refer to the member function. You did this correctly for the constructor and destructor in the .cpp file, now you need to do it to makeJournal and displayJournal as well.

And as Ed S. said, it is wrong to include .cpp files in .h files. Rather, you link your source files together using your compiler.

Upvotes: 3

Related Questions