C++关于敲代码时遇到的一个与new有关问题

刚刚在敲代码的时候遇到一个问题,我定义了一个头文件,代码如下

typedef struct _prop
{
	int id;
	char* name;				//商品名
	double price;			//商品单价
	int stock;					//商品库存
	char* desc;				//商品描述
}Prop;//商品
typedef struct _player
{
	int id;						//玩家编号
	char* name;				//用户名称
	char* password;		//用户密码
	Bag bag;					//玩家背包
	double gold;			//玩家金钱
	double sysee;			//玩家元宝
}Player;
void Initdate(Prop* props,Player* players)
{
	props = new Prop[5];//开辟num个Prop结构的内存空间(问题代码)
	players = new Player[5];//开辟num个Player结构的内存空间(问题代码)
	int i = 0;
	for (int j = 0; j < 5; j++)
	{
		(props + j)->id = j + 1;
	}//初始化物品id
	for (int j = 0; j < 5; j++)
	{
		(players + j)->id = j + 1;
	}//初始化玩家id
	//初始化物品属性
	(props + i)->name = "双倍经验卡";
	(props + i)->desc = "经验加成";
	(props + i)->price = 3000.0;
	(props + i)->stock = 10;
	i++;
	(props + i)->name = "幸运宝石";
	(props + i)->desc = "幸运道具";
	(props + i)->price = 5000.0;
	(props + i)->stock = 20;
	i++;
	(props + i)->name = "五行转换卡";
	(props + i)->desc = "五行转换";
	(props + i)->price = 5000.0;
	(props + i)->stock = 20;
	i++;
	(props + i)->name = "银两袋子";
	(props + i)->desc = "银两";
	(props + i)->price = 10000.0;
	(props + i)->stock = 20;
	i++;
	(props + i)->name = "高级灵珠宝箱";
	(props + i)->desc = "高级灵珠";
	(props + i)->price = 100000.0;
	(props + i)->stock = 50;
	i = 0;//重新将i初始化
	//初始化玩家属性
	/*
		int id;						//玩家编号
		char* name;				//用户名称
		char* password;		//用户密码
		Bag bag;					//玩家背包
		double gold;			//玩家金钱
		double sysee;			//玩家元宝
	*/
	(players + i)->name = "野满横江";
	(players + i)->gold = 11111111.00;
	(players + i)->password = "admin";
	(players + i)->sysee = 11111111.00;
	i++;
	(players + i)->name = "野";
	(players + i)->gold = 22222222.00;
	(players + i)->password = "admin";
	(players + i)->sysee = 22222222.00;
	i++;
	(players + i)->name = "满";
	(players + i)->gold = 33333333.00;
	(players + i)->password = "admin";
	(players + i)->sysee = 33333333.00;
	i++;
	(players + i)->name = "横";
	(players + i)->gold = 44444444.00;
	(players + i)->password = "admin";
	(players + i)->sysee = 44444444.00;
	i++;
	(players + i)->name = "江";
	(players + i)->gold = 55555555.00;
	(players + i)->password = "admin";
	(players + i)->sysee = 55555555.00;//初始化玩家数据
	//ShowProps(props);
	return ;
}
void ShowProps(Prop* props)
{
	if (props != NULL)
	{
		std::cout << "编号\t名称\t单价\t库存\t商品介绍" << std::endl;
		for (int i = 0; i < 5; i++)
		{
			std::cout << (props + i)->id << "\t" << (props + i)->name << "\t" << (props + i)->price << "\t" << (props + i)->stock << "\t" << (props + i)->desc << std::endl;
		}
	}
}

然后我尝试用main函数调用它,代码如下

#include <iostream>
#include "shop.h"
//int PropCount = 0;
//int PlayerCount = 0;
 Prop* props;//物品
 Player* players;//玩家
int main()
{
	Initdate(props, players);//初始化数据
	ShowProps(props);
	return 0;
}

正常情况下应该运行后能够正确打印出初始化的数据,但代码运行后直接就返回0了,我猜测是props的地址指向了NULL,调试后发现确实是这样,但我依然不清楚这个问题出现在哪里,我尝试在Initdate函数的内部调用ShowProps函数,运行后发现数据可以正常打印出来。根据这样的情况,我猜测也许是指针的传递问题,但又经过一系列的调式后发现,原来是两段new代码的问题,于是我将这两行代码直接剪切到main函数的头两行后,发现问题解决了,(猜测可能是cpp文件中我只是声明了结构指针,但并没有把这两个指针指向地址的原因)但我仍不清楚这其中问题的原理,希望有大佬能够指点一二,小弟不胜感谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的决策树的C++代码示例: ```c++ #include <iostream> #include <vector> using namespace std; // 节点结构体 struct Node { bool is_leaf; // 是否为叶子节点 int feature; // 特征索引 vector<Node*> children; // 子节点 int label; // 叶子节点的类别标签 }; // 训练决策树 Node* train(vector<vector<int>>& data, vector<int>& labels, vector<int>& features) { Node* node = new Node; int num_samples = data.size(); int num_features = features.size(); // 计算样本中各个类别的数量 vector<int> label_counts(2); for (int i = 0; i < num_samples; i++) { label_counts[labels[i]]++; } // 如果所有样本都属于同一类别,返回叶子节点 if (label_counts[0] == num_samples) { node->is_leaf = true; node->label = 0; return node; } if (label_counts[1] == num_samples) { node->is_leaf = true; node->label = 1; return node; } // 如果没有剩余特征可用,返回叶子节点 if (num_features == 0) { node->is_leaf = true; node->label = label_counts[0] > label_counts[1] ? 0 : 1; return node; } // 选择最佳特征 int best_feature_index; double best_information_gain = -1; for (int i = 0; i < num_features; i++) { int feature_index = features[i]; vector<int> left_label_counts(2), right_label_counts(2); int left_count = 0, right_count = 0; for (int j = 0; j < num_samples; j++) { if (data[j][feature_index] == 0) { left_label_counts[labels[j]]++; left_count++; } else { right_label_counts[labels[j]]++; right_count++; } } double left_entropy = 0, right_entropy = 0; if (left_count > 0) { double left_p0 = (double)left_label_counts[0] / left_count; double left_p1 = (double)left_label_counts[1] / left_count; if (left_p0 > 0) { left_entropy -= left_p0 * log2(left_p0); } if (left_p1 > 0) { left_entropy -= left_p1 * log2(left_p1); } } if (right_count > 0) { double right_p0 = (double)right_label_counts[0] / right_count; double right_p1 = (double)right_label_counts[1] / right_count; if (right_p0 > 0) { right_entropy -= right_p0 * log2(right_p0); } if (right_p1 > 0) { right_entropy -= right_p1 * log2(right_p1); } } double information_gain = ((double)left_count / num_samples * left_entropy) + ((double)right_count / num_samples * right_entropy); if (information_gain > best_information_gain) { best_information_gain = information_gain; best_feature_index = feature_index; } } // 创建非叶子节点 node->is_leaf = false; node->feature = best_feature_index; vector<int> left_data_indices, right_data_indices; for (int i = 0; i < num_samples; i++) { if (data[i][best_feature_index] == 0) { left_data_indices.push_back(i); } else { right_data_indices.push_back(i); } } vector<int> left_features, right_features; for (int i = 0; i < num_features; i++) { if (features[i] != best_feature_index) { left_features.push_back(features[i]); right_features.push_back(features[i]); } } node->children.push_back(train(data, labels, left_features)); node->children.push_back(train(data, labels, right_features)); return node; } // 预测 int predict(Node* node, vector<int>& sample) { if (node->is_leaf) { return node->label; } int feature_index = node->feature; if (sample[feature_index] == 0) { return predict(node->children[0], sample); } else { return predict(node->children[1], sample); } } int main() { // 构造训练数据 vector<vector<int>> data = { {1, 0, 0}, {1, 0, 1}, {0, 1, 0}, {0, 1, 1}, {1, 1, 0}, {1, 1, 1}, {0, 0, 0}, {0, 0, 1} }; vector<int> labels = {0, 1, 1, 0, 1, 1, 0, 1}; vector<int> features = {0, 1, 2}; // 训练决策树 Node* root = train(data, labels, features); // 预测 vector<int> sample = {1, 0, 1}; int prediction = predict(root, sample); cout << "Prediction: " << prediction << endl; return 0; } ``` 以上代码实现了一个简单的二分类决策树,训练数据是一个3维特征的8个样本,其中前两维特征是布尔类型,最后一维特征是整数类型。特征选择采用信息增益法,分类标准是样本所属的类别。预测,给定一个样本,从根节点开始遍历,根据特征值选择左/右子树,直到遇到叶子节点,返回类别标签。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值