You can't initialize a variable like this:
You can only define it at that stage. If there were a class definition, you could make a class constructor to fix it. I'm not sure how to fix this without digging into the book.Code:int Action2 = 0;
Todd
Printable View
You can't initialize a variable like this:
You can only define it at that stage. If there were a class definition, you could make a class constructor to fix it. I'm not sure how to fix this without digging into the book.Code:int Action2 = 0;
Todd
Yes I just figured out as I got your answer so I deleted the code just before.
So I have compile success when declaring it like: int Action2
But I only have this If I do this with the line that do the sort for Values:
//std::sort(Values.begin(), Values.end());
So something must be wrong here. The thing is that I will sort for the Action value. Perheps this should be specified in any way.
Code:#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
struct Value
{
int Action2;
std::string Symbol2;
};
using namespace std;
int main ()
{
char Comma;
int t = 0;
std::string Symbol;
int Action = 0;
std::vector<Value> Values (3);
ofstream Sort1;
Sort1.open ("Sort1.txt");
ifstream Sort ("Sort.txt");
while ( getline(Sort, Symbol, ',') )
{
t = (t + 1); // Count
Sort >> Action; // Buy
Values[t].Symbol2 = Symbol;
Values[t].Action2 = Action;
}
//std::sort(Values.begin(), Values.end());
for (int Rowsen = 1; Rowsen < (t + 1); Rowsen++) //Number of Lines
{
Sort1 <<
Values[Rowsen].Symbol2 << ',' <<
Values[Rowsen].Action2
<< "\n";
}
return 0;
}
The problem is that C++ doesn't know how to sort these new-fangled Value objects, since you haven't told it how. You're going to have to define what < means for Values. Someone will chime in if I'm wrong, but I believe you need to define this function:
(Technically you can call it anything you want, but if you call it something else you have to specify that function in your sort call.)Code:bool operator < (const Value& lhs, const Value& rhs) {
// returns true if lhs < rhs, false otherwise
// code goes here
}
This way the sort algorithm knows whether this guy is less than that one.
Yes that right ofcourse... I will check these functions out and laborate with this and see what I get... Thanks for your help !
I am trying to do a sort so the contents in a .txt file that look like this:
ABC,3
DEF,2
will look like this: (Ascending order for the number) (The sorting is for the numbers(Called Action2 in my code)
DEF,2
ABC,3
I using stucts to manage this but have a problem to understand what I should do with the these lines that is doing the rules for the sort and then sorting it.
I beleive it should be written "Bool operator" and if the x value is less than the y value inside the parathes Bool evaluates TRUE. I think the logics stands for this.
Then it is many things I dont understand, like if the values x and y is exampleletters or should it stand something else here in my case ?
The next line with return is the same, what should be written instead of x.Values ?
For the sort that coming next.
Should it only be written Values.begin() or do I have to refer to Action2 ?
Code:bool operator < (const Value& x, const Value& y)
{
return x.Values < y.Values
};
std::sort(Values.begin(), Values.end());
Code:#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
struct Value
{
int Action2;
std::string Symbol2;
};
using namespace std;
int main ()
{
char Comma;
int t = 0;
std::string Symbol;
int Action = 0;
std::vector<Value> Values (3);
ofstream Sort1;
Sort1.open ("Sort1.txt");
ifstream Sort ("Sort.txt");
while ( getline(Sort, Symbol, ',') )
{
t = (t + 1); // Count
Sort >> Action; // Buy
Values[t].Symbol2 = Symbol;
Values[t].Action2 = Action;
}
bool operator < (const Value& x, const Value& y)
{
return x.Values < y.Values
};
std::sort(Values.begin(), Values.end());
for (int Rowsen = 1; Rowsen < (t + 1); Rowsen++) //Number of Lines
{
Sort1 <<
Values[Rowsen].Symbol2 << ',' <<
Values[Rowsen].Action2
<< "\n";
}
return 0;
}
And why do you think x.Values means anything? x is a Value -- your function declaration says so! -- so x contains a Symbol2 and an Action2. Same with y.
And of course you can call your parameters of your functions anything you like, provided they aren't keywords like if, or macros. I like to use lhs and rhs, but no one's going to complain about x and y.
Okay, so if x and y contains the Action2 and Symbol2 it makes a little bit more sense to me.
So far that I can manage right now, I think I have to specify what value in the x and y values that is interesting for sorting.. In this case Action2. So I write: x.Action2 < y.Action2
I dont know if this is correct:
The compiler says that:Code:bool operator < (const Value& x, const Value& y)
{
return x.Action2 < y.Action2;
};
std::sort(Values.begin(), Values.end());
'operator <' : local function definitions are illegal
a '{' which has not yet been matched
Don't define functions within other functions.
Even if I change this line to this. Still there is something that isn´t correct. The Compiler isn´t happy.
Code:{
return x.Action2 < y.Action2
};
You are implementing the function "operator<" inside another function (main) in the code you posted above. You need to move this function out of there and above main (or below, if you declare a prototype above).
--
Mats
Thank You matsp... This was the mainproblem I had. I didn´t know that this function had to be ouside the main. So now it is working. Couldn´t understand what was wrong... Thanks...
In standard C or C++ (rather than extensions such as gcc), you can NEVER define a function inside another function. Some other languages, such as Pascal, allows you to define a function inside another function - but C and C++ doesn't.
You can define a class member function inside a class, that is just about the only time you can declare a function inside of curly brackets.
--
Mats
Yes okay I see. That was what I wondered also. I knew that Pascal could do it so I was a bit confused about it. Now I have a lot to experiment on :-)