473,795 Members | 3,393 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why this code can't be compiled??

The code is below:
#include <iostream>
#include <functional>

template <typename T, typename FUN>
void insert_sort(T *arr, size_t size, FUN f = std::less<T>())
{
for( int j = 1; j < size; j++ ){
type key = arr[j];
int i = j - 1;
while( i >= 0 && f(arr[i], key) ){
arr[i+1] = arr[i];
i = i - 1;
}
arr[i+1] = key;
}
}

template <typename T>
void display(std::os tream &out, const T *arr, size_t size)
{
out << "<";
for(size_t i = 0; i < size; ++i){
out << arr[i] << ((i == size - 1) ? ">" : " ");
}
}

int main()
{
const int iarr[] = { 9, 8, 56, 784, 2, 7, 89, 4 };
const double darr[] = { 7.8, 89.2, 45.0, 44.0, 9.6, 78.4, 999.0 };
int *piarr = new int[sizeof(iarr) / sizeof(int)];
double *pdarr = new double[sizeof(darr) / sizeof(double)];

std::cout << "before sort:\n";
display(std::co ut, iarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(piarr, iarr, sizeof(iarr));
insert_sort(pia rr, sizeof(iarr) / sizeof(int));
std::cout << "after insort sort:\n";
display(std::co ut, piarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(pdarr, darr, sizeof(darr));
insert_sort(pda rr, sizeof(darr) / sizeof(double)) ;
std::cout << "after insort sort:\n";
display(std::co ut, pdarr, sizeof(darr) / sizeof(int));

delete[] piarr;
delete[] pdarr;

return 0;
}

What's wrong??
Why the default arguement doesn't work??

Thanks.

Regards
Wayne Shu

Nov 9 '06 #1
8 1551

Wayne Shu wrote:
Why this code can't be compiled??
Because it's not legal C++.
The code is below:
#include <iostream>
#include <functional>

template <typename T, typename FUN>
void insert_sort(T *arr, size_t size, FUN f = std::less<T>())
{
for( int j = 1; j < size; j++ ){
type key = arr[j];
What is "type"? It's not defined.
int i = j - 1;
while( i >= 0 && f(arr[i], key) ){
arr[i+1] = arr[i];
i = i - 1;
}
arr[i+1] = key;
}
}

template <typename T>
void display(std::os tream &out, const T *arr, size_t size)
{
out << "<";
for(size_t i = 0; i < size; ++i){
out << arr[i] << ((i == size - 1) ? ">" : " ");
}
}

int main()
{
const int iarr[] = { 9, 8, 56, 784, 2, 7, 89, 4 };
const double darr[] = { 7.8, 89.2, 45.0, 44.0, 9.6, 78.4, 999.0 };
int *piarr = new int[sizeof(iarr) / sizeof(int)];
double *pdarr = new double[sizeof(darr) / sizeof(double)];

std::cout << "before sort:\n";
display(std::co ut, iarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(piarr, iarr, sizeof(iarr));
insert_sort(pia rr, sizeof(iarr) / sizeof(int));
There's no way for the compiler to deduce the type of FUN here. Change
the above line to:

insert_sort<int , std::less<int(p iarr, sizeof(iarr) /
sizeof(int));
std::cout << "after insort sort:\n";
display(std::co ut, piarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(pdarr, darr, sizeof(darr));
insert_sort(pda rr, sizeof(darr) / sizeof(double)) ;
Again, there's no way for the compiler to deduce the type of FUN here.
Change the above line to:

insert_sort<dou ble, std::less<doubl e(pdarr, sizeof(darr) /
sizeof(double)) ;
std::cout << "after insort sort:\n";
display(std::co ut, pdarr, sizeof(darr) / sizeof(int));

delete[] piarr;
delete[] pdarr;

return 0;
}

What's wrong??
Why the default arguement doesn't work??
Because the compiler can't deduce the type of FUN using the default
argument when it depends upon the type of another template parameter.
Thanks.
Best regards,

Tom

Nov 9 '06 #2
Wayne Shu wrote:
The code is below:
#include <iostream>
#include <functional>

template <typename T, typename FUN>
void insert_sort(T *arr, size_t size, FUN f = std::less<T>())
{
for( int j = 1; j < size; j++ ){
type key = arr[j];
int i = j - 1;
while( i >= 0 && f(arr[i], key) ){
arr[i+1] = arr[i];
i = i - 1;
}
arr[i+1] = key;
}
}

template <typename T>
void display(std::os tream &out, const T *arr, size_t size)
{
out << "<";
for(size_t i = 0; i < size; ++i){
out << arr[i] << ((i == size - 1) ? ">" : " ");
}
}

int main()
{
const int iarr[] = { 9, 8, 56, 784, 2, 7, 89, 4 };
const double darr[] = { 7.8, 89.2, 45.0, 44.0, 9.6, 78.4, 999.0 };
int *piarr = new int[sizeof(iarr) / sizeof(int)];
double *pdarr = new double[sizeof(darr) / sizeof(double)];

std::cout << "before sort:\n";
display(std::co ut, iarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(piarr, iarr, sizeof(iarr));
insert_sort(pia rr, sizeof(iarr) / sizeof(int));
std::cout << "after insort sort:\n";
display(std::co ut, piarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(pdarr, darr, sizeof(darr));
insert_sort(pda rr, sizeof(darr) / sizeof(double)) ;
std::cout << "after insort sort:\n";
display(std::co ut, pdarr, sizeof(darr) / sizeof(int));

delete[] piarr;
delete[] pdarr;

return 0;
}

What's wrong??
Why the default arguement doesn't work??

Thanks.

Regards
Wayne Shu
What error do you get when you try to compile?
Nov 9 '06 #3
VJ
Thomas Tutone wrote:
>>The code is below:
#include <iostream>
#include <functional>

template <typename T, typename FUN>
void insert_sort(T *arr, size_t size, FUN f = std::less<T>())
{
for( int j = 1; j < size; j++ ){
type key = arr[j];


What is "type"? It's not defined.

Yes, instead of type should put T

>
> int i = j - 1;
while( i >= 0 && f(arr[i], key) ){
arr[i+1] = arr[i];
i = i - 1;
}
arr[i+1] = key;
}
}

template <typename T>
void display(std::os tream &out, const T *arr, size_t size)
{
out << "<";
for(size_t i = 0; i < size; ++i){
out << arr[i] << ((i == size - 1) ? ">" : " ");
}
}

int main()
{
const int iarr[] = { 9, 8, 56, 784, 2, 7, 89, 4 };
const double darr[] = { 7.8, 89.2, 45.0, 44.0, 9.6, 78.4, 999.0 };
int *piarr = new int[sizeof(iarr) / sizeof(int)];
double *pdarr = new double[sizeof(darr) / sizeof(double)];

std::cout << "before sort:\n";
display(std::co ut, iarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(piarr, iarr, sizeof(iarr));
insert_sort(pia rr, sizeof(iarr) / sizeof(int));


There's no way for the compiler to deduce the type of FUN here. Change
the above line to:

insert_sort<int , std::less<int(p iarr, sizeof(iarr) /
sizeof(int));
This gives next warning:

taking address of temporary

Anyone know a way to remove it?
Nov 9 '06 #4
"VJ" <v@asd.orgwro te in message
news:ei******** **@el-srv04-CHE.srvnet.east link.de...
Thomas Tutone wrote:
>>>The code is below:
#include <iostream>
#include <functional>

template <typename T, typename FUN>
void insert_sort(T *arr, size_t size, FUN f = std::less<T>())
{
for( int j = 1; j < size; j++ ){
type key = arr[j];


What is "type"? It's not defined.


Yes, instead of type should put T

>>
>> int i = j - 1;
while( i >= 0 && f(arr[i], key) ){
arr[i+1] = arr[i];
i = i - 1;
}
arr[i+1] = key;
}
}

template <typename T>
void display(std::os tream &out, const T *arr, size_t size)
{
out << "<";
for(size_t i = 0; i < size; ++i){
out << arr[i] << ((i == size - 1) ? ">" : " ");
}
}

int main()
{
const int iarr[] = { 9, 8, 56, 784, 2, 7, 89, 4 };
const double darr[] = { 7.8, 89.2, 45.0, 44.0, 9.6, 78.4, 999.0 };
int *piarr = new int[sizeof(iarr) / sizeof(int)];
double *pdarr = new double[sizeof(darr) / sizeof(double)];

std::cout << "before sort:\n";
display(std:: cout, iarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(piar r, iarr, sizeof(iarr));
insert_sort(p iarr, sizeof(iarr) / sizeof(int));


There's no way for the compiler to deduce the type of FUN here. Change
the above line to:

insert_sort<int , std::less<int(p iarr, sizeof(iarr) /
sizeof(int)) ;

This gives next warning:

taking address of temporary

Anyone know a way to remove it?
sizeof( iarr[0] ) maybe?
Nov 9 '06 #5

"Thomas Tutone дµÀ£º
"
Wayne Shu wrote:
Why this code can't be compiled??

Because it's not legal C++.
The code is below:
#include <iostream>
#include <functional>

template <typename T, typename FUN>
void insert_sort(T *arr, size_t size, FUN f = std::less<T>())
{
for( int j = 1; j < size; j++ ){
type key = arr[j];

What is "type"? It's not defined.
sorry, "type" should be "T", I forgot to modify it.
>
int i = j - 1;
while( i >= 0 && f(arr[i], key) ){
arr[i+1] = arr[i];
i = i - 1;
}
arr[i+1] = key;
}
}

template <typename T>
void display(std::os tream &out, const T *arr, size_t size)
{
out << "<";
for(size_t i = 0; i < size; ++i){
out << arr[i] << ((i == size - 1) ? ">" : " ");
}
}

int main()
{
const int iarr[] = { 9, 8, 56, 784, 2, 7, 89, 4 };
const double darr[] = { 7.8, 89.2, 45.0, 44.0, 9.6, 78.4, 999.0 };
int *piarr = new int[sizeof(iarr) / sizeof(int)];
double *pdarr = new double[sizeof(darr) / sizeof(double)];

std::cout << "before sort:\n";
display(std::co ut, iarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(piarr, iarr, sizeof(iarr));
insert_sort(pia rr, sizeof(iarr) / sizeof(int));

There's no way for the compiler to deduce the type of FUN here. Change
the above line to:

insert_sort<int , std::less<int(p iarr, sizeof(iarr) /
sizeof(int));
std::cout << "after insort sort:\n";
display(std::co ut, piarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(pdarr, darr, sizeof(darr));
insert_sort(pda rr, sizeof(darr) / sizeof(double)) ;

Again, there's no way for the compiler to deduce the type of FUN here.
Change the above line to:

insert_sort<dou ble, std::less<doubl e(pdarr, sizeof(darr) /
sizeof(double)) ;
std::cout << "after insort sort:\n";
display(std::co ut, pdarr, sizeof(darr) / sizeof(int));

delete[] piarr;
delete[] pdarr;

return 0;
}

What's wrong??
Why the default arguement doesn't work??

Because the compiler can't deduce the type of FUN using the default
argument when it depends upon the type of another template parameter.
If I want to the default argument work, how to change the code??
Thanks.
Best regards,

Tom
Nov 10 '06 #6
Wayne Shu wrote:
"Thomas Tutone дµÀ£º
Wayne Shu wrote:
Why this code can't be compiled??
Because it's not legal C++.
[snip]
What's wrong??
Why the default arguement doesn't work??
Because the compiler can't deduce the type of FUN using the default
argument when it depends upon the type of another template parameter.
If I want to the default argument work, how to change the code??
I have no idea whether your code is correct, but the way to make the
default argument work is to overload the template to have another
version of insert_sort that only takes two arguments. Try this:

#include <iostream>
#include <functional>

template <typename T, typename Fun>
void insert_sort(T *arr, size_t size, Fun f)
{
for( int j = 1; j < size; j++ ){
T key = arr[j];
int i = j - 1;
while( i >= 0 && f(arr[i], key) ) {
arr[i+1] = arr[i];
i = i - 1;
}
arr[i+1] = key;
}
}

template <typename T>
inline void insert_sort(T *arr, size_t size)
{
insert_sort(arr , size, std::less<T>()) ;
}
template <typename T>
void display(std::os tream &out, const T *arr, size_t size)
{
out << "<";
for(size_t i = 0; i < size; ++i){
out << arr[i] << ((i == size - 1) ? ">" : " ");
}
}

int main()
{
const int iarr[] = { 9, 8, 56, 784, 2, 7, 89, 4 };
const double darr[] = { 7.8, 89.2, 45.0, 44.0, 9.6, 78.4, 999.0
};
int *piarr = new int[sizeof(iarr) / sizeof(int)];
double *pdarr = new double[sizeof(darr) / sizeof(double)];

std::cout << "before sort:\n";
display(std::co ut, iarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(piarr, iarr, sizeof(iarr));
insert_sort(pia rr, sizeof(iarr) / sizeof(int));
std::cout << "after insort sort:\n";
display(std::co ut, piarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(pdarr, darr, sizeof(darr));
insert_sort(pda rr, sizeof(darr) / sizeof(double)) ;
std::cout << "after insort sort:\n";
display(std::co ut, pdarr, sizeof(darr) / sizeof(int));

delete[] piarr;
delete[] pdarr;
}

Best regards,

Tom

Nov 10 '06 #7
Hello,

Wayne Shu wrote:
template <typename T, typename FUN>
void insert_sort(T *arr, size_t size, FUN f = std::less<T>())
What's wrong??
Why the default arguement doesn't work??
What you basically intended is a default template parameter. (You even
wanted it to be deduced through a default argument to a function
parameter.)

template <typename T, typename FUN = std::less<T>>
void insert_sort(T *arr, size_t size, FUN f = FUN())

But this is clearly illegal, my compiler says:

error: default template arguments may not be used in function templates

This is state of the art in the current standard, and according to the
book "C++ Templates" by Vandevoorde and Josuttis, it is being worked
on. Currently, the template parameters of a function template must be
deducible from the call expression.

You can overload as T. Tutone suggested. The nice point is that this can
be done uniformly with a few lines. If the standard gets extended to
allow default parameters for function templates those trivial overloads
may disappear again. I don't know whether this case can be really
considered, because it could become hairy to mix template argument
deduction, and function overload resolution that much.

template <typename T, typename FUN>
void insert_sort(T *arr, size_t size, FUN f)
{...
}

template <typename T>
void insert_sort(T *arr, size_t size)
{
insert_sort(arr , size, std::less<T>);
}

Bernd Strieder

Nov 10 '06 #8
thanks, I forgot that we can the trick of overload function
"Bernd Strieder дµÀ£º
"
Hello,

Wayne Shu wrote:
template <typename T, typename FUN>
void insert_sort(T *arr, size_t size, FUN f = std::less<T>())
What's wrong??
Why the default arguement doesn't work??

What you basically intended is a default template parameter. (You even
wanted it to be deduced through a default argument to a function
parameter.)

template <typename T, typename FUN = std::less<T>>
void insert_sort(T *arr, size_t size, FUN f = FUN())

But this is clearly illegal, my compiler says:

error: default template arguments may not be used in function templates

This is state of the art in the current standard, and according to the
book "C++ Templates" by Vandevoorde and Josuttis, it is being worked
on. Currently, the template parameters of a function template must be
deducible from the call expression.

You can overload as T. Tutone suggested. The nice point is that this can
be done uniformly with a few lines. If the standard gets extended to
allow default parameters for function templates those trivial overloads
may disappear again. I don't know whether this case can be really
considered, because it could become hairy to mix template argument
deduction, and function overload resolution that much.

template <typename T, typename FUN>
void insert_sort(T *arr, size_t size, FUN f)
{...
}

template <typename T>
void insert_sort(T *arr, size_t size)
{
insert_sort(arr , size, std::less<T>);
}

Bernd Strieder
Nov 11 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3921
by: Novice | last post by:
Hi all, I'm afraid this is the second posting of this information as I didn't get a response on the previous post. I will try to shorten my message (i.e. be more concise) in the hopes that it will make it easier for someone (i.e. a Microsoft person) to digest the information and respond to it. I am a C++ and Java developer with over 3 years of industry experience. I've written low level C++ code, in addition to web clients that use web...
13
2051
by: AFKAFB | last post by:
Hi Sometimes when i edit previously saved VBA code e.g. To update field names etc the revised code does not work. Even if i copy and paste in a previous version it does not work. The only solution seems to be to delete the existing code, re-type ir word for word and hey presto it works. Of course you might think typo or something but its like Access seems
3
1974
by: Göran Carlefyr | last post by:
Hi everyone, i've heard that aspx-files can be compiled code so that the webserver just have to deal with the code once, is that true? I'm a complete newbie to .NET, i've never made anything in ASP.NET but i've been coding ASP for approx. 3 years. The reason I'm asking is because I have this menu that have x numbers of database connections and it takes like one second after the graphics before the menu appears.
37
5996
by: Alan Silver | last post by:
Hello, Newbie here, so please forgive what is probably a basic question ... I see a lot of discussion about "code behind", which if I have understood correctly, means that the script code goes in a separate file from the HTML. Apart from the obvious advantage if you have a separate designer and programmer, are there any other advantages to code behind? Most of the stuff I've seen so far uses code inside, but that's probably
171
7800
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles) like it because you can split the code from the design. That is logical. But if you are the only one working on the code, it seem a little overkill. I use Dreamweaver to do my design and find it a bit of a hassle to have multiple files open...
21
2149
by: Sandy | last post by:
Hello - I am using Visual Studio .Net. I need an example of how to construct a class that can be used throughout a project where I can include its subs and functions in various pages in the project. I googled this and either I didn't use the correct words, or there doesn't seem to be much on it. Any help will be appreciated!
2
1376
by: Bit byte | last post by:
I have some IP (intellectual property) concerns regarding IL code. 1). If I compile a native (i.e. 'unmanaged') C++ project (i.e. application or library) using C++ and the VC7.1 compiler tools, how can I be sure that it is native code (not IL) that is generated? 2). Is native code generated by default for native C++ code projects?
2
2286
by: CodeMonkey775 | last post by:
I'm having problems passing a variable to a method which is executed and compiled using CodeDom. The situation is I have a List<CellData> with cells, each containing a formula (like Excel). I am trying to pass this list to an executable section of code which is compiled during run-time. The formulas are calculated in CodeDom and the result is returned on the same List<CellData> object I passed. The user-defined class which holds the cell data...
15
2076
by: colemanj4 | last post by:
Here is what I have so far, it loops while the PW is incorrect, or until cancel is selected. I want it to lock the tables for adds, deletes, and edits when cancel is selected, and if the PW is correct. I want this to run when the DB is opened, I would also like the text box to be starred (*) out when text is input. Thanks for your help. ==============================
8
1308
by: Max | last post by:
Is it possible for an ASP.net server (like IIS 6) to run web application without having available the code behind forms in clear ascii? Can I provide a compiled product to the customer, not an exe file of course, but some dll compiled by my server ? I'm asking that because 1. sometimes I am not happy to provide my source code 2.customer is afraid of their internal security... Thanks Max
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10437
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.