[英]C++ How to declare a vector in the header, set size in constructor, then access
I am having issues declaring a vector in the header, setting the default size in the constructor, then making it a function to do stuff in aDie.cpp.我在标题中声明一个向量,在构造函数中设置默认大小,然后使其成为在 aDie.cpp 中执行操作的函数时遇到问题。 My known error is under the aDie.cpp declaration is incompatible for
void aDie::Frequency(std::vector<int> Vector());
我的已知错误是 aDie.cpp 声明与
void aDie::Frequency(std::vector<int> Vector());
不兼容void aDie::Frequency(std::vector<int> Vector());
. . Any help would be appreciated to get me going in the right direction so that I have a vector that I can add values into.
任何帮助都会让我朝着正确的方向前进,这样我就有了一个可以添加值的向量。 My code is below.
我的代码如下。
header aDie.h标题 aDie.h
#define aDie_H
#include <vector>
class aDie {
public:
int i = 0;
aDie();
void numRolls();
void getSeed();
void roll();
void Frequency(std::vector<int> Vector());
private:
int Rolls;
int dSeed;
int die1;
int die2;
int sum;
};
cpp aDie.cpp cpp aDie.cpp
#include "aDie.h"
#include <iostream>
#include <ctime>
#include <vector>
using namespace std;
aDie::aDie() { //constructor sets values to default 0
die1 = 0;
die2 = 0;
dSeed = 0;
vector<int> Vector(6); //defaults to size six
}
void aDie::numRolls() {
cout << "Enter number of times for die to be rolled: ";
cin >> Rolls;
return;
}
void aDie::getSeed() { //gets seed
cout << "Enter desired seed number: ";
cin >> dSeed;
srand(dSeed);
return;
}
void aDie::roll() { //rng for one die
die1 = 1 + rand() % 6;
}
void aDie::Frequency(std::vector<int> Vector()); //store the rolls output by aDie::roll
}
.cpp Main.cpp .cpp 主.cpp
#include <iostream>
using namespace std;
#include "aDie.h"
#include "aHistogram.h"
#include <iostream>
#include <vector>
using namespace std;
void game1();
int main() {
game1();
system("Pause");
}
void game1() {
int i = 0;
int Rolls = 0;
cout << "Enter number of times for die to be rolled: ";
cin >> Rolls;
aDie Roll1; //calls default constructor
Roll1.getSeed(); //calls seed
for (i = 0; i < Rolls; i++) {
Roll1.roll(); //rolls die
Roll1.//whatever my vector will be called
}
}
The code编码
void Frequency(std::vector<int> Vector());
is indeed invalid syntax, what you probably want to do is确实是无效的语法,您可能想要做的是
void Frequency(std::vector<int> &v);
or或者
void Frequency(const std::vector<int> &v);
If you want to store the vector, then it should be member of the class:如果要存储向量,则它应该是该类的成员:
class aDie {
//...
private:
int Rolls;
int dSeed;
int die1;
int die2;
int sum;
std::vector<int> vect;
};
and then the constructor could look like:然后构造函数看起来像:
aDie::aDie()
//constructor sets values to default 0
: die1(0)
, die2(0)
, dSeed(0)
, vect(6) //defaults to size six
{}
I'm not sure if this is your other doubt.我不确定这是否是您的另一个疑问。
In header:在标题中:
class aDie {
public:
int i = 0;
vector<int> Vector;
aDie();
void numRolls();
void getSeed();
void roll();
void Frequency(const vector<int>& Vector);
private:
int Rolls;
int dSeed;
int die1;
int die2;
int sum;
};
In .cpp:在 .cpp 中:
aDie::aDie() { //constructor sets values to default 0
die1 = 0;
die2 = 0;
dSeed = 0;
Vector = vector<int>(6); //defaults to size six
}
I recommend you start reading up on the basics eg here (especially chapter 8).我建议您开始阅读基础知识,例如这里(尤其是第 8 章)。
But to answer your question, I try to give you a short walkthrough in the comments of the code.但是为了回答你的问题,我试着在代码的注释中给你一个简短的演练。
// Header file A.h
#include <vector>
class A {
public:
A();
void addNumbers();
void addNumberIntoAnyVector(std::vector<int>& vec);
private:
std::vector<int> myVector;
};
// A.cpp
#include "A.h"
// First option, initialize the member before the body executes.
// See e.g. http://www.cprogramming.com/tutorial/initialization-lists-c++.html
A::A() : myVector(6) {
}
// Second option, resize it inside the body of you contructor
// Use only one of them, not both!
A::A() {
myVector.resize(6);
}
// You have access to your member variables from within any member function
// So simply numbers to your myVector here.
void A::addNumbers() {
for(int i=0; i<myVector.size(); ++i)
myVector[i] += i;
}
// In this version you can pass any vector<int> instance as an argument
// and fill it with numbers.
void addNumberIntoAnyVector(std::vector<int>& vec) {
for(int i=0; i < vec.size(); ++i)
vec[i] += i;
}
If that doesn't make sense to you, you should go back even further in the tutorials.如果这对您没有意义,您应该在教程中更进一步。 I would really suggest to get some foundational knowledge first.
我真的建议先获得一些基础知识。
If I understand correctly, you want to store the results of die rolls and their associated frequencies of occurrence in a container?如果我理解正确,您想将掷骰结果及其相关的发生频率存储在容器中吗? If so, then this should achieve what you want (ignoring irrelevant methods):
如果是这样,那么这应该可以实现您想要的(忽略不相关的方法):
#define A_DIE_H
#ifndef A_DIE_H
#include <map> // for std::map
#include <utility> // for std::pair / std::make_pair
class aDie {
public:
aDie();
void roll();
int getFrequency(int _res) const;
private:
// stores die roll results as Keys and frequency
// of occurrence as Values
std::map<int, int> result_freq_map;
};
#endif /* A_DIE_H */
and in the implementation file:并在实现文件中:
#include "aDie.h"
aDie::aDie() {
}
void aDie::roll() { //rng for one die
int die1 = 1 + rand() % 6; // roll the dice
// try inserting result into map with occurrence of 1
if (result_freq_map.insert(std::make_pair(die1,1)).second) {
return;
}
// if result already contained in map, increment its associated value
result_freq_map[die1]++;
}
// gets the frequency of occurrence of a given result _res
int aDie::getFrequency(int _res) const {
return result_freq_map[_res];
}
This is untested but it should work.这是未经测试的,但它应该可以工作。 I can address the rest of your code and how it could be structured better if you like as well.
如果您愿意,我可以解决您的其余代码以及如何更好地构建它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.