被class构造函数和初始化列表搞糊涂了,怀疑是循环依赖
Confused by class constructor and initializer list, suspicious of circular dependency
我想通过另一个 class 的构造函数传递一个 class 的引用实例。现在我不能这样做,因为我陷入了语法错误。我尝试了几个小时,虽然我学到了很多东西(例如循环依赖或前向声明),但实际上无法解决我的问题。我有这个文件:
Project.h(向 Project class 的构造函数传递了 Buffer class 的引用实例)
Project.cpp
Buffer.h
Buffer.cpp
以上四个文件在我的问题中是活跃的(我的猜测)。
这里是Project.h内容:
#include<string>
#include<regex>
#include<iostream>
#include<vector>
#include "Buffer.h"
using namespace boost::filesystem;
#ifndef PROJECT_H
#define PROJECT_H
class Project(Buffer & buffer_param) : bufferObj(buffer_param)
{
Buffer& bufferObj;
public:
Filer& filer;
std::string project_directory;
void createList(std::vector<std::string> list_title);
};
#endif
这里是project.cpp内容:
#include<string>
#include<regex>
#include<iostream>
#include<vector>
#include<boost\filesystem.hpp>
#include "Project.h"
using namespace boost::filesystem;
void Project::createList(std::vector<std::string> list_title)
{
//this->filer.createFile();
}
Buffer.h
#include<string>
#include<map>
#ifndef BUFFER_H
#define BUFFER_H
class Buffer
{
std::map<std::string, std::string> storage_str;
void setValueString(std::string key, std::string value);
std::string getValueString(std::string key);
};
#endif
Buffer.cpp
#include<string>
#include<map>
#include "Buffer.h"
void Buffer::setValueString(std::string key, std::string value)
{
this->storage_str[key] = value;
}
问题:
在不将缓冲区传递给项目构造函数的情况下,一切正常,但是一旦我开始传递它的实例,就会抛出错误:
Project.h
文件的所有错误:
error C2143: syntax error : missing ')' before '&'
error C2143: syntax error : missing ';' before '&'
error C2079: 'Buffer' uses undefined class 'Project'
error C2059: syntax error : ')'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2530: 'buffer_param' : references must be initialized
error C2143: syntax error : missing ';' before ':'
error C2448: 'bufferObj' : function-style initializer appears to be a function definition
Project.cpp
文件错误:
error C2027: use of undefined type 'Project'
see declaration of 'Project'
详细说明 deviantfan 的评论,而不是这个
class Project(Buffer & buffer_param) : bufferObj(buffer_param)
{
...
应该是这样的:
class Project
{
public:
Project(Buffer & buffer_param) : bufferObj(buffer_param)
{
}
private:
...
我想通过另一个 class 的构造函数传递一个 class 的引用实例。现在我不能这样做,因为我陷入了语法错误。我尝试了几个小时,虽然我学到了很多东西(例如循环依赖或前向声明),但实际上无法解决我的问题。我有这个文件:
Project.h(向 Project class 的构造函数传递了 Buffer class 的引用实例)
Project.cpp
Buffer.h
Buffer.cpp
以上四个文件在我的问题中是活跃的(我的猜测)。
这里是Project.h内容:
#include<string>
#include<regex>
#include<iostream>
#include<vector>
#include "Buffer.h"
using namespace boost::filesystem;
#ifndef PROJECT_H
#define PROJECT_H
class Project(Buffer & buffer_param) : bufferObj(buffer_param)
{
Buffer& bufferObj;
public:
Filer& filer;
std::string project_directory;
void createList(std::vector<std::string> list_title);
};
#endif
这里是project.cpp内容:
#include<string>
#include<regex>
#include<iostream>
#include<vector>
#include<boost\filesystem.hpp>
#include "Project.h"
using namespace boost::filesystem;
void Project::createList(std::vector<std::string> list_title)
{
//this->filer.createFile();
}
Buffer.h
#include<string>
#include<map>
#ifndef BUFFER_H
#define BUFFER_H
class Buffer
{
std::map<std::string, std::string> storage_str;
void setValueString(std::string key, std::string value);
std::string getValueString(std::string key);
};
#endif
Buffer.cpp
#include<string>
#include<map>
#include "Buffer.h"
void Buffer::setValueString(std::string key, std::string value)
{
this->storage_str[key] = value;
}
问题:
在不将缓冲区传递给项目构造函数的情况下,一切正常,但是一旦我开始传递它的实例,就会抛出错误:
Project.h
文件的所有错误:
error C2143: syntax error : missing ')' before '&'
error C2143: syntax error : missing ';' before '&'
error C2079: 'Buffer' uses undefined class 'Project'
error C2059: syntax error : ')'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2530: 'buffer_param' : references must be initialized
error C2143: syntax error : missing ';' before ':'
error C2448: 'bufferObj' : function-style initializer appears to be a function definition
Project.cpp
文件错误:
error C2027: use of undefined type 'Project'
see declaration of 'Project'
详细说明 deviantfan 的评论,而不是这个
class Project(Buffer & buffer_param) : bufferObj(buffer_param)
{
...
应该是这样的:
class Project
{
public:
Project(Buffer & buffer_param) : bufferObj(buffer_param)
{
}
private:
...