|
Avi,
Weird, as I just created a test console app (using VS2005) and it builds without errors:
#include "stdafx.h"
#include <vector>
#include <algorithm>
struct item_calculation
{
int item_num;
float weight;
float contribution;
float prediction;
};
bool less_than(const item_calculation& a, const item_calculation& b)
{
return a.prediction < b.prediction;
};
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<item_calculation> items;
std::sort(items.begin(), items.end(), less_than);
}
|
|
|
|
|
Thanks for the effort, Robert.
This problem buggs me exactly because the code seems to me to be correct.
In my case all the declarations (struct, function) are made inside a class definition, and sort() is called from inside a member fonction. can this make any difference?
|
|
|
|
|
avimitrani wrote: In my case all the declarations (struct, function) are made inside a class definition, and sort() is called from inside a member fonction. can this make any difference?
Yes. Move the overriden operator< outside of your app class and declare it as a friend to your app class.
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
class MyClass
{
public:
MyClass() { initMe(); }
void sortMe() { sort(found_items.begin(), found_items.end()); }
void showMe() { copy(found_items.begin(), found_items.end(), ostream_iterator<item_calculation>(cout, " ")); }
private:
struct item_calculation
{
int item_num;
float weight;
float contribution;
float prediction;
item_calculation() : item_num(0), weight(0.0), contribution(0.0), prediction(0.0)
{}
item_calculation(int in, float w, float c, float p) : item_num(in), weight(w), contribution(c), prediction(p)
{}
};
vector<item_calculation> found_items;
void initMe()
{
for (int i = 10; i > 0; --i)
{
float f = static_cast<float>(i);
item_calculation ic(i, f, f, f);
found_items.push_back(ic);
}
}
friend bool operator< (const item_calculation& lhs, const item_calculation& rhs);
friend ostream& operator<<(ostream& os, const item_calculation& ic);
};
bool operator< (const MyClass::item_calculation& lhs, const MyClass::item_calculation& rhs)
{
return lhs.prediction < rhs.prediction;
}
ostream& operator<<(ostream& os, const MyClass::item_calculation& ic)
{
os << ic.item_num;
return os;
}
int main()
{
MyClass me;
me.showMe();
cout << endl;
me.sortMe();
me.showMe();
}
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Thanks again, Zac
Your suggestion strikes me as the most correct way to do it.
I managed to compile the project by using a different solution:
struct item_calculation
{<br />
int item_num;<br />
float weight;<br />
float contribution;<br />
float prediction;<br />
bool operator<(const item_calculation &b) {<br />
return prediction < b.prediction;<br />
};<br />
};
This should work too, but I'll opt for the friend function option.
Many thanks,
Avi.
|
|
|
|
|
The only problem you will run into is if the item_calculation struct is a private inner class, the opeartor< will be "private", so it won't be able to be used outside the outer class (that is, sort may have issues with it). That goes away if it is a public inner class, or if you override the operators outside the class.
There are other reasons to override binary operators outside the class they operate on, but that goes beyond the scope of this question.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Your code just needs to be modified slightly:
struct item_calculation
{
int item_num;
float weight;
float contribution;
float prediction;
item_calculation() : item_num(0), weight(0.0), contribution(0.0), prediction(0.0)
{}
};
bool operator< (const item_calculatioin& lhs, const item_calculation& rhs)
{
return lhs.prediction < rhs.prediction;
}
sort(found_items.begin(), found_items.end());
-- modified at 10:49 Tuesday 12th September, 2006
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
what is the meaning of the added lines:
item_calculation() : item_num(0), weight(0.0), contribution(0.0), prediction(0.0) <br />
{}
Thanks,
Avi.
|
|
|
|
|
it initializes those variables
|
|
|
|
|
Why should they be initialized?
(this is the only difference between your code and mine)
|
|
|
|
|
In C++, class and struct are pretty much the same keyword (class defaults to private protection, while struct defaults to public -- that is pretty much the only major difference).
Thus, you can create constructors for structs just as you would a class. The code below creates a default constructor and initializes each variable to a value (in this case, all 0's).
avimitrani wrote: item_calculation() : item_num(0), weight(0.0), contribution(0.0), prediction(0.0)
{}
It is almost the same as writing it this way:
item_calculation()
{
item_num = 0;
weight = 0.0;
contribution = 0.0;
prediction = 0.0;
}
except that the first case is more efficient because the members are created and initialzed at the same time.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Yes, but why should initialization make a difference? Before I use sort() I loop over the vector, and fill thr struct fields.
|
|
|
|
|
Initialization is important, but that won't fix your problem.
The important differences between your code and mine are bolded:
struct item_calculation
{
int item_num;
float weight;
float contribution;
float prediction;
item_calculation() : item_num(0), weight(0.0), contribution(0.0), prediction(0.0)
{}
};
bool operator< (const item_calculatioin& lhs, const item_calculation& rhs)
{
return lhs.prediction < rhs.prediction;
}
sort(found_items.begin(), found_items.end());
Having the operator outside the class definition is a better way (and least likely to run into problems when using algorithms) to define it. For classes, you may want to declare them as friend functions as needed.
There is no need to define a new less_than method. By default, sort calls the less<T> functor which calls the overriden operator< for and object of type T (in this case, item_calculation).
Looking back at your original post, it looks like you were trying to declare item_calculation as a private inner class to your App class. Am I correct?
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
I want to read information from database but when I want to put
them in a listControl i have an error message
impossible to convert paramètre 3 from 'CStringW' to 'CString'
thank you
|
|
|
|
|
What version of Visual Studio/MFC/ATL are you using?
In VS2003/VS2005 you can convert from an ASCII to Unicode string using the following conversion function:
CStringA ascii_string = "Hello, world!";
CStringW wide_string = CA2W(ascii_string);
|
|
|
|
|
I use
Microsoft Visual C++ .NET 55604-652-0000007-18431 version 7.0
when i put the line you gave me i had this error
'cast de type' : impossible de convertir de 'CStringW' en 'ATL::CA2WEX<t_nbufferlength>'
with
[
t_nBufferLength=128
]
|
|
|
|
|
Starnage as it works fine for me in an MFC app built using VS2005.
|
|
|
|
|
try this link
http://www.flounder.com/cstring.htm
Rinu Raj
|
|
|
|
|
the function i use is:
m_listCtrl.AddString(person.m_Nom+ " " + person.m_prenom);
person is the class of my database, m_Nom is a recordSet
addString accept CString who shall i use the function CA2W
is it like this !
CStringW s = CA2W(person.m_Nom);
m_listCtrl.AddString(s)
|
|
|
|
|
zizzzz wrote: m_listCtrl.AddString(person.m_Nom+ " " + person.m_prenom);
person is the class of my database, m_Nom is a recordSet
If m_Nom is a CRecordset , you obviously cannot use it as a CString .
"Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
ti barra yé sou... rana fey9in bik
salem ,c wiem.
|
|
|
|
|
|
Nothing, I Wrote it by a wrong way:->
salem ,c wiem.
|
|
|
|
|
|
I have just recompiled a VC6 project in Visual Studio 2005 - the port seemed to go ok and it compiled but when I try to run it under the debugger it just bombs out with the following:
'Tyke.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', No symbols loaded.<br />
'Tyke.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', No symbols loaded.<br />
'Tyke.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugMFC_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_c8452471\mfc80d.dll', Symbols loaded.<br />
'Tyke.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_f75eb16c\msvcr80d.dll', Symbols loaded.<br />
'Tyke.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', No symbols loaded.<br />
'Tyke.exe': Loaded 'C:\WINDOWS\system32\gdi32.dll', No symbols loaded.<br />
'Tyke.exe': Loaded 'C:\WINDOWS\system32\user32.dll', No symbols loaded.<br />
'Tyke.exe': Loaded 'C:\WINDOWS\system32\shlwapi.dll', No symbols loaded.<br />
'Tyke.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll', No symbols loaded.<br />
'Tyke.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll', No symbols loaded.<br />
'Tyke.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_f75eb16c\msvcp80d.dll', Symbols loaded.<br />
'Tyke.exe': Loaded 'C:\WINDOWS\system32\oleaut32.dll', No symbols loaded.<br />
'Tyke.exe': Loaded 'C:\WINDOWS\system32\ole32.dll', No symbols loaded.<br />
'Tyke.exe': Loaded 'C:\WINDOWS\system32\odbc32.dll', No symbols loaded.<br />
'Tyke.exe': Loaded 'C:\WINDOWS\system32\comctl32.dll', No symbols loaded.<br />
'Tyke.exe': Loaded 'C:\WINDOWS\system32\shell32.dll', No symbols loaded.<br />
'Tyke.exe': Loaded 'C:\WINDOWS\system32\comdlg32.dll', No symbols loaded.<br />
'Tyke.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.MFCLOC_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_3415f6d0\mfc80ENU.dll', Binary was not built with debug information.<br />
'Tyke.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9\comctl32.dll', No symbols loaded.<br />
'Tyke.exe': Loaded 'C:\WINDOWS\system32\odbcint.dll', Binary was not built with debug information.<br />
The program '[3284] Tyke.exe: Native' has exited with code 0 (0x0).
Any ideas why this would be?
|
|
|
|
|
these are not error... just warnings.
the "Binary was not built with debug information." info just tells you that you won't be able to debug the code which wasn't built in debug mode... that's all
|
|
|
|
|