Click here to Skip to main content
15,892,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have been trying to code to find the smallest and second smallest number in an array by using sort and classes and object but i have been getting the error sort function should have been a prototype. Please help me correct it . thankyou.

What I have tried:

#include<stdio.h>
#include<iostream.h>
#include<conio.h>

class asc
{int a[100],i,n;
public:
void input();
void display();
};
void asc::input()
{cout<<"Enter the size of array"<<endl;
cin>>n;
cout<<"Enter the array elements"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
}
void asc::display()
{
sort(a[0],a[n-1]);
cout<<"Smallest number is "<<a[0]<<endl;
cout<<"second smallest is  "<<a[1]<<endl;
};
void main()
{ asc a;
clrscr();
a.input();
a.display();
getch();
}
Posted
Updated 13-Jul-22 22:50pm
Comments
0x01AA 13-Jul-22 15:18pm    
Maybe just forgot #include <algorithm>?

Quote:
I have been getting the error sort function should have been a prototype.
Under C++, many functions are in the std namespace. To do this, you must either use using statements or call up the functions with the namespace specified. Examples would be std::cout or std::sort. The <algorithm> header must also be included for the sort() function.

The std::sort function also requires an iterable container when sorting, whose elements have a comparison function "operator<()".

The C-style array used here is inappropriate for several reasons. It is not automatically initialized, nor does it have an iterator. It is better to use a std::vector or a std::array here.
C++
class asc {
public:
    void input();
    void display();
private:
	//int a[100];
    std::array<int, 100> a;
    int i, n;
};

#include <stdio.h>
#include <iostream.h>
#include <conio.h>

The headers look like an example for (legacy) Turbo C++. From today's perspective, this is very outdated and can no longer be used for most current systems. There are some problems with this includes. The header <iostream.h> does not exist at most current systems. The C++ headers are usually included without ".h". The <stdio.h> header is an old C header and should better not be used with C++. Functions from <iostream> should preferably be used. The <conio.h> header is not part of the C Standard Library or ISO-C and is not portable as it does not exist on most Linux systems.

en.wikipedia.org/wiki/Conio.h

Replacements for the Clrscr() and Getch() functions are discussed here:
cplusplus.com/articles/4z18T05o/
 
Share this answer
 
v2
Your compiler is trying to tell you that you need a definition for the sort() function. Normally, this is found in the algorithm header. However, it appears that you are using Turbo C++, which does not seem to include any sort functions.

Turbo C++ (at least the free download version (3.0, (c)1992), predates the STL and normal, modern C++ practices. As such, it's practically useless as both a learning tool and a serious development tool. Just about any web site out there will assume that you have a full STL implementation (c++ 98), and many will expect you to have C++ 11, or even C++ 20 to be able to follow along.

Unless you're constrained to a DOS box, or have very limited download capability, you should investigate the community version of Visual Studio. Its a free version, and the latest versions can compile C++ 20. If you are constrained to DOS, for some reason, you might want to look into DJCPP[^] as an alternative to Turbo C++
 
Share this answer
 
Comments
0x01AA 13-Jul-22 15:43pm    
5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900