int和vector"未在此范围内声明"

时间:2014-10-02 03:18:01

标签: c++

这是我在SO的第一篇文章。我无法编译这个。它一直说在此范围内未声明journalKeyjournalKeyCount。它还说我的构造函数,析构函数和我的所有函数都是“重新定义”。

这是.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

这是.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;
}

提前感谢您的建议。

更新

·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;
}

错误日志要短得多,但我还是得到了:

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|

你们这些人很快,很有帮助= D

1 个答案:

答案 0 :(得分:3)

在类外定义成员函数时,需要使用嵌套名称说明符journal::来引用成员函数。您正确地为.cpp文件中的构造函数和析构函数执行了此操作,现在您需要将其执行到makeJournaldisplayJournal

正如Ed S.所说,在.h文件中包含.cpp文件是错误的。相反,您使用编译器链接源文件。