让一个 class 使用另一个的对象
Having one class using objects from another
我有一个 class,我想包含另一个 class CRandomSFMT
的对象(我自己没有写)。这是 test.h
:
using namespace std;
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include "sfmt.h"
class Something {
public:
Something();
private:
int seed;
CRandomSFMT rangen();
void seed_generator(int, CRandomSFMT&);
};
这是test.cpp
:
#include "test.h"
Something::Something() {
seed=1;
seed_generator(seed, rangen);
}
void Something::seed_generator(int seed, CRandomSFMT& rangen) {
rangen.RandomInit(seed);
}
当我尝试用 g++ -c sfmt.o -msse2 -std=c++11 -O2 test.cpp
编译它时,我得到
test.cpp: In constructor ‘Something::Something()’:
test.cpp:5:30: error: invalid use of non-static member function
seed_generator(seed, rangen);
我试图声明 seed_generator()
静态,但这没有帮助。这是 class CRandomSFMT
:
的声明
class CRandomSFMT {
public:
CRandomSFMT(int seed, int IncludeMother = 0) {
UseMother = IncludeMother;
LastInterval = 0;
RandomInit(seed);}
void RandomInit(int seed);
void RandomInitByArray(int const seeds[], int NumSeeds);
int IRandom (int min, int max);
int IRandomX (int min, int max);
double Random();
uint32_t BRandom();
private:
void Init2();
void Generate();
uint32_t MotherBits();
uint32_t ix;
uint32_t LastInterval;
uint32_t RLimit;
uint32_t UseMother;
__m128i mask;
__m128i state[SFMT_N];
uint32_t MotherState[5];
};
有什么想法吗?
您正在声明一个函数,而不是一个对象:
CRandomSFMT rangen();
既然你想把它当作一个对象来使用,应该是这样的:
CRandomSFMT rangen;
您已将 rangen
声明为一个函数;错误是因为你试图像使用对象一样使用它。
根据您的描述,"I want to contain an object",它应该是一个对象而不是函数:
CRandomSFMT rangen; // no ()
这必须使用其构造函数进行初始化:
Something::Something() : seed(1), rangen(seed) {}
我有一个 class,我想包含另一个 class CRandomSFMT
的对象(我自己没有写)。这是 test.h
:
using namespace std;
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include "sfmt.h"
class Something {
public:
Something();
private:
int seed;
CRandomSFMT rangen();
void seed_generator(int, CRandomSFMT&);
};
这是test.cpp
:
#include "test.h"
Something::Something() {
seed=1;
seed_generator(seed, rangen);
}
void Something::seed_generator(int seed, CRandomSFMT& rangen) {
rangen.RandomInit(seed);
}
当我尝试用 g++ -c sfmt.o -msse2 -std=c++11 -O2 test.cpp
编译它时,我得到
test.cpp: In constructor ‘Something::Something()’:
test.cpp:5:30: error: invalid use of non-static member function
seed_generator(seed, rangen);
我试图声明 seed_generator()
静态,但这没有帮助。这是 class CRandomSFMT
:
class CRandomSFMT {
public:
CRandomSFMT(int seed, int IncludeMother = 0) {
UseMother = IncludeMother;
LastInterval = 0;
RandomInit(seed);}
void RandomInit(int seed);
void RandomInitByArray(int const seeds[], int NumSeeds);
int IRandom (int min, int max);
int IRandomX (int min, int max);
double Random();
uint32_t BRandom();
private:
void Init2();
void Generate();
uint32_t MotherBits();
uint32_t ix;
uint32_t LastInterval;
uint32_t RLimit;
uint32_t UseMother;
__m128i mask;
__m128i state[SFMT_N];
uint32_t MotherState[5];
};
有什么想法吗?
您正在声明一个函数,而不是一个对象:
CRandomSFMT rangen();
既然你想把它当作一个对象来使用,应该是这样的:
CRandomSFMT rangen;
您已将 rangen
声明为一个函数;错误是因为你试图像使用对象一样使用它。
根据您的描述,"I want to contain an object",它应该是一个对象而不是函数:
CRandomSFMT rangen; // no ()
这必须使用其构造函数进行初始化:
Something::Something() : seed(1), rangen(seed) {}