整数和矢量"was not declared in this scope"
int and vector "was not declared in this scope"
这是我在SO的第一篇文章。我很难编译它。它一直说journalKey
和journalKeyCount
没有在这个范围内声明。它还说我的构造函数、析构函数和我所有的函数都是"重定义"。
这是.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文件中的构造函数和析构函数执行了正确的操作,现在还需要对makeJournal
和displayJournal
执行此操作。
正如Ed S.所说,在.h文件中包含.cpp文件是错误的。相反,您可以使用编译器将源文件链接到一起。
相关文章:
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 类中的数组变量C++导致"was not declared in this scope"实现文件的一个函数中错误,但在构造函数中不会导致错误
- 如何解决"cout was not declared in this scope"错误?
- 为什么程序显示我"was not declared in this scope"?
- 为什么我会收到"getuid was not declared in that scope"错误?
- 即使包含 <time.h 后仍"‘CLOCK_PER_SEC’ was not declared in this scope"错误>
- 将数据添加到 CArray 会产生错误"cannot access private member declared in class 'CObject'"
- 我正在接受"Error: string was not declared in this scope"
- C++ 我一直在"was not declared in this scope error"
- 将 .ui 文件导入 Qt5 C++时出错"was not declared in this scope"
- 编译 CPP 应用程序时出错。 "error: 'posix_memalign' was not declared in this scope"
- main.cpp中的错误:函数库中的函数"not declared in this scope",但实际上是
- "Error: 'dungeon_t' was not declared in this scope"但包含在标题中
- C++ 中的" Variable ' i ' was not declared in scope "是什么?
- "pointer was not declared in this scope"是什么意思
- 学习如何在遇到错误'variable not declared in scope'的 c++ 中使用指针
- "error: not declared in this scope?"是什么意思
- 宣布时的另一个'not declared in this scope'问题
- " 'X' not declared in this scope "错误
- 子类模板错误 (C++): "error: 'root' was not declared in this scope"