整数和矢量"was not declared in this scope"

int and vector "was not declared in this scope"

本文关键字:declared in this scope not was 整数      更新时间:2023-10-16

这是我在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

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

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