C ++指针问题,请帮帮我! [英] C++ pointer problem,please help me!

查看:86
本文介绍了C ++指针问题,请帮帮我!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
using namespace std;

template<typename t="">
void insertion_sort(T* a){
	for(size_t i = 0; i < sizeof(a)/sizeof(a[0]); i++){
		T *tmp = &a[i];
		while(i>0 && (a[i-1] > *tmp)){
			*a[i] = *a[i-1];
			i--;
		}
		a[i] = *tmp;   //error: invalid array assignment 
	}
}

int main(){
	int test[] = {31,23,45,67,12,3,55,78,35,24,98};
	insertion_sort(&test);
	for(size_t j = 0;j		cout << test[j];
	}
	return 0;
}



此代码出现错误:



this code get a error:

a[i] = *tmp;   //error: invalid array assignment 


谁能告诉我问题出在哪里,

[edit]整理出的代码块,<和>已修复-OriginalGriff [/edit]


can anybody tell me what''s the problem,thank!

[edit]Code block sorted out, < and > fixed - OriginalGriff[/edit]

推荐答案

没有通过调试器运行代码-我强烈建议您这样做-我怀疑您的问题在这里:sizeof(a)/sizeof(a[0])
由于您将a声明为T* a,因此sizeof(a)将始终是指针的大小:对于32位系统,为4.
Without running your code through the debugger - which I strongly recommend you do - I suspect your problem is here: sizeof(a)/sizeof(a[0])
Since you declared a as T* a, sizeof(a) will always be the size of a pointer: 4 in the case of a 32 bit system.


看看模板部分专业化",第二个例子.我认为您要尝试的操作与在此处找到的操作相同,请告诉我们是否有帮助.
http://www.cprogramming.com/tutorial/template_specialization.html [
Take a look to "Template Partial Specialization",the second example. I think what are you trying to do is the same you find here, and let us know if this was helpfull.
http://www.cprogramming.com/tutorial/template_specialization.html[^]


这篇关于C ++指针问题,请帮帮我!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆