C++编程问题(编译出错)

happiness2080 2008-11-27 09:47:47
#include "iostream.h"
#include <string.h>

class QuickSort{
private:
int *theArray;
int elementNumber;
// int max;

public:
int partition();
QuickSort(int max){
long *theArray=new long[max] ;
elementNumber=0;
}
void insert(int value){
theArray[elementNumber]=value;
elementNumber++;
}
void display(){
for(int j=0;j<elementNumber;j++)
cout<<theArray[j]<<endl;
cout<<"";
}
void quickSort(){
recQuickSort(0,elementNumber-1);
}
void recQuickSort(int low,int high){
if(high-low<=0)
return;
else{
long pivot=theArray[high];
int partition1;
partition1=partition(low,high,pivot);
recQuickSort(low,partition-1);
recQuickSort(partition1+1,high);
}
}

int partition(int low,int high,long pivot)
{
int lowPtr=low;
int highPtr=high-1;
while(true){
while(theArray[lowPtr]<pivot)
lowPtr++;
while(highPtr>0&&theArray[highPtr]>pivot)
highPtr--;
if(lowPtr>=highPtr)
break;
else
swap(lowPtr,highPtr);

swap(lowPtr,high);
return lowPtr;
}
}
void swap(int data1,int data2){
long temp=theArray[data1];
theArray[data1]=theArray[data2];
theArray[data2]=temp;
}
};

void main()
{
QuickSort array1;
int n;
cout<<"也请输入元素个数:"<<endl;
cin>>n;
cout<<endl;
array1=new QuickSort(n);
cout<<"请输入"<<n<<"个元素"<<endl;
for(int i=0;i<n;i++)
{
cin>>i;
cout<<endl;
array1.insert(i);
}
array1.display();
array1.quickSort();
array1.display();
}

...全文
210 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
happiness2080 2008-11-27
  • 打赏
  • 举报
回复
多谢各位的指示!
thank you!
wzg112 2008-11-27
  • 打赏
  • 举报
回复
或者直接在if中return
wzg112 2008-11-27
  • 打赏
  • 举报
回复
需要在所有出口加上return;

if (lowPtr>=highPtr)
break;//这里跳出了while循环,所以要在while循环外加return;
cyj626 2008-11-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hacker1125 的回复:]
引用 6 楼 happiness2080 的回复:
warning C4715: 'QuickSort::partition' : not all control paths return a value
请问,提示这个,是什么意思?

提示没有返回值。
你定义的partition需要返回一个int类型
[/Quote]
这个算法有问题啊
hacker1125 2008-11-27
  • 打赏
  • 举报
回复
#include "iostream.h"
#include <string.h>

class QuickSort{
private:
int *theArray;
int elementNumber;
// int max;

public:
int partition();
QuickSort(int max)
{
long *theArray=new long[max] ;
elementNumber=0;
}
void insert(int value){
theArray[elementNumber]=value;
elementNumber++;
}
void display(){
for(int j=0;j <elementNumber;j++)
cout <<theArray[j] <<endl;
cout <<"";
}
void quickSort(){
recQuickSort(0,elementNumber-1);
}
void recQuickSort(int low,int high){
if(high-low <=0)
return;
else{
long pivot=theArray[high];
int partition1;
partition1=partition(low,high,pivot);
recQuickSort(low,partition-1);
recQuickSort(partition1+1,high);
}
}

int partition(int low,int high,long pivot)
{
int lowPtr=low;
int highPtr=high-1;
while(true){
while(theArray[lowPtr] <pivot)
lowPtr++;
while(highPtr>0&&theArray[highPtr]>pivot)
highPtr--;
if(lowPtr>=highPtr)
break;
else
swap(lowPtr,highPtr);

swap(lowPtr,high);
return lowPtr;
}
return 0;//没有返回int会有警告,但可编译通过
}
void swap(int data1,int data2){
long temp=theArray[data1];
theArray[data1]=theArray[data2];
theArray[data2]=temp;
}
};

void main()
{
QuickSort *array1; //修改 下面是申请存放指针,所以要定义指针对象。
int n;
cout <<"也请输入元素个数:" <<endl;
cin>>n;
cout <<endl;
array1=new QuickSort(n);
cout <<"请输入" <<n <<"个元素" <<endl;
for(int i=0;i <n;i++)
{
cin>>i;
cout <<endl;
array1->insert(i);
}
array1->display(); //使用指针,所以用->
array1->quickSort();
array1->display();
}
jiww03 2008-11-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 happiness2080 的回复:]
warning C4715: 'QuickSort::partition' : not all control paths return a value
请问,提示这个,是什么意思?
[/Quote]
有的流程逻辑没有返回值呗,比如
if(...)
{
return 0;
}
else
{
//没有return;
}
lann64 2008-11-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 happiness2080 的回复:]
warning C4715: 'QuickSort::partition' : not all control paths return a value
请问,提示这个,是什么意思?
[/Quote]就是说有的分支没有到达return 语句
hacker1125 2008-11-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 happiness2080 的回复:]
warning C4715: 'QuickSort::partition' : not all control paths return a value
请问,提示这个,是什么意思?
[/Quote]
提示没有返回值。
你定义的partition需要返回一个int类型
happiness2080 2008-11-27
  • 打赏
  • 举报
回复
warning C4715: 'QuickSort::partition' : not all control paths return a value
请问,提示这个,是什么意思?
lann64 2008-11-27
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
using namespace std;

class QuickSort
{
private:
long *theArray;//
int elementNumber;
// int max;

public:
int partition();
QuickSort(int max)
{
theArray=new long[max] ;
elementNumber=0;
}
void insert(int value)
{
theArray[elementNumber]=value;
elementNumber++;
}
void display()
{
for (int j=0;j <elementNumber;j++)
cout <<theArray[j] <<endl;
cout <<"";
}
void quickSort()
{
recQuickSort(0,elementNumber-1);
}
void recQuickSort(int low,int high)
{
if (high-low <=0)
return;
else
{
long pivot=theArray[high];
int partition1;
partition1=partition(low,high,pivot);
recQuickSort(low,partition1-1); ///
recQuickSort(partition1+1,high);
}
}

int partition(int low,int high,long pivot)
{
int lowPtr=low;
int highPtr=high-1;
while (true)
{
while (theArray[lowPtr] <pivot)
lowPtr++;
while (highPtr>0&&theArray[highPtr]>pivot)
highPtr--;
if (lowPtr>=highPtr)
break;
else
swap(lowPtr,highPtr);

swap(lowPtr,high);
return lowPtr; //这个逻辑对吗?
}
}
void swap(int data1,int data2)
{
long temp=theArray[data1];
theArray[data1]=theArray[data2];
theArray[data2]=temp;
}
};

int main()
{
// QuickSort array1;
int n;
cout <<"也请输入元素个数:" <<endl;
cin>>n;
cout <<endl;
QuickSort array1(n);///
cout <<"请输入" <<n <<"个元素" <<endl;
for (int i=0;i <n;i++)
{
cin>>i;
cout <<endl;
array1.insert(i);
}
array1.display();
array1.quickSort();
array1.display();
}


编译可以了,可你的程序逻辑上好像有错。
CPlusPlusFans 2008-11-27
  • 打赏
  • 举报
回复

#include "stdafx.h"
#include <iostream>
#include <string.h>
using namespace std;
class QuickSort{
private:
long *theArray;
int elementNumber;
// int max;

public:
QuickSort(int max){
theArray=new long[max] ;
elementNumber=0;
}
void insert(int value){
theArray[elementNumber]=value;
elementNumber++;
}
void display(){
for(int j=0;j <elementNumber;j++)
cout <<theArray[j] <<endl;
cout <<"";
}
void quickSort(){
recQuickSort(0,elementNumber-1);
}
void recQuickSort(int low,int high){
if(high-low <=0)
return;
else{
long pivot=theArray[high];
int partition1;
partition1=partition(low,high,pivot);
recQuickSort(low,partition1-1);
recQuickSort(partition1+1,high);
}
}

int partition(int low,int high,long pivot)
{
int lowPtr=low;
int highPtr=high-1;
while(true){
while(theArray[lowPtr] <pivot)
lowPtr++;
while(highPtr>0&&theArray[highPtr]>pivot)
highPtr--;
if(lowPtr>=highPtr)
break;
else
swap(lowPtr,highPtr);

swap(lowPtr,high);
return lowPtr;
}
}
void swap(int data1,int data2){
long temp=theArray[data1];
theArray[data1]=theArray[data2];
theArray[data2]=temp;
}
};

void main()
{
QuickSort *array1;
int n;
cout <<"也请输入元素个数:" <<endl;
cin>>n;
cout <<endl;
array1=new QuickSort(n);
cout <<"请输入" <<n <<"个元素" <<endl;
for(int i=0;i <n;i++)
{
cin>>i;
cout <<endl;
array1->insert(i);
}
array1->display();
array1->quickSort();
array1->display();
}

happiness2080 2008-11-27
  • 打赏
  • 举报
回复
G:\学习\C++\QuickSort2\QuickSort.cpp(67) : error C2512: 'QuickSort' : no appropriate default constructor available
G:\学习\C++\QuickSort2\QuickSort.cpp(72) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class QuickSort *' (or there is no acceptable conversion)
执行 cl.exe 时出错.
stanboy 2008-11-27
  • 打赏
  • 举报
回复
把错误也贴上来
lgccaa 2008-11-27
  • 打赏
  • 举报
回复
错误信息贴出来啊

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧