Yep, that did it, thanks. Forgot about the const iterators.
Sorry I forgot to include "using namepace std;"
Dave
Yep, that did it, thanks. Forgot about the const iterators.
Sorry I forgot to include "using namepace std;"
Dave
I am trying to use the for_each from stl algorithm.
http://www.cplusplus.com/reference/algorithm/for_each.html
#include <algorithm>
#include <vector>
#include <iostream>
template <typename T>
void OutputObject(const T &obj)
{
cout << obj << endl;
}
template <typename T>
void OutputVector(const vector<T> &V)
{
for_each (V.begin(), V.end(), OutputObject);
cout << endl;
}
I get "no matching function call to for_each". Why would that be?
Thanks,
Dave
I'm just trying to figure out how this stuff works. I wrote this function:
#include <algorithm>
#include <vector>
double VectorMax(const vector<double> &a)
{
vector<double>::iterator pos;
pos = max_element (a.begin(), a.end());
return *pos;
}
and call it with:
vector<double> Numbers;
Numbers.push_back(3.4);
Numbers.push_back(4.5);
Numbers.push_back(1.2);
cout << VectorMax(Numbers) << endl;
but I get "no match for operator=" error. Since this is just a container of doubles, shouldn't = already be defined?
Thanks,
Dave
I found this while googling for something
http://msdn.microsoft.com/en-us/library/ms177203(VS.80).aspx
I tried it (with g++) and it seems to be a syntax error - is this like something specific to Visual Studio or something?
Dave
What? What would a static function do here?
I was talking about "should I make a derived class INSTEAD of each of the constructors I currently have".
I have tried the "make an init() function and call it from all the constructors", but that is almost equally annoying. What is this 1-2-3 constructor you speak of? So you are saying just make a class
class OrientedPoint
{
private:
Point P;
Vector N;
Color C;
public:
OrientedPoint(const Point &p){P = p;}
void setColor(const Color &c){C = c;}
void setNormal(const Vector &n){N = n;}
};
And call those set() function when you need more things? But then I have no way of knowing if those things have been set and they will end up with junk values. Thats why I wanted to initialize them to 0 or something so I will at least be able to tell if they have been set. See what I mean?
So you're recommending that I derive classes for each of the constructors I currently have?
I often have this situation
class OrientedPoint
{
private:
Point P;
Vector N;
Color C;
bool valid;
public:
//////////// Constructors //////////
OrientedPoint() {}
OrientedPoint(const Point &Coord);
OrientedPoint(const Point &Coord, const Vector &Normal);
OrientedPoint(const Point &Coord, const Color &C);
OrientedPoint(const Point &Coord, const Vector &Normal, const Color &C);
where there are many, many constructors to initialize the class when different quantities are available at the time of construction. What I want to do is always set valid to false, without having to say "valid = false" in every constructor, or putting valid(false) in each constructor's initialization list. The idea is that since this code is very fluid, I don't want to change the same thing in 10 places. Is there anyway to do something that would be like
class OrientedPoint
{
private:
Point P;
Vector N;
Color C;
bool valid = false; //always init this variable to a certain value!
All ideas welcome!
Thanks,
Dave
Is there a reason to use
cout << static_cast<int>(thing);
instead of
cout << (int)thing;
?
Thanks,
Dave
nucleon - unfortunately I'd like to keep it in the same structure because I have many other functions that work with this 2d array.
I put together a stand alone example:
#include <iostream>
using namespace std;
void FixedLength();
void VariableLength();
void FillAndRead(unsigned char* buffer, const unsigned int Width, const unsigned int Height);
int main()
{
FixedLength();
VariableLength();
return 0;
}
void FillAndRead(unsigned char* buffer, const unsigned int Width, const unsigned int Height)
{
//fill
for(unsigned int x = 0; x < Width; x++)
{
for(unsigned int y = 0; y < Height; y++)
{
buffer[x][y][0] = 3;
buffer[x][y][1] = 4;
buffer[x][y][2] = 5;
}
}
//read
unsigned char r,g,b;
for(unsigned int x = 0; x < Width; x++)
{
cout << endl;
for(unsigned int y = 0; y < Height; y++)
{
r = buffer[x][y][0];
g = buffer[x][y][1];
b = buffer[x][y][2];
cout << r << " " << g << " " << b << " | ";
}
}
cout << endl;
}
void FixedLength()
{
unsigned int Width = 100;
unsigned int Height = 100;
unsigned char buffer[100][100][3];
FillAndRead(buffer, Width, Height);
}
void VariableLength()
{
unsigned int Width = 100;
unsigned int Height = 100;
//Want to do this: unsigned char bufImage[Width][Height][3];
unsigned char*** buffer = new unsigned char**[Width];
for(unsigned int i = 0; i < Width; ++i)
{
buffer[i] = new unsigned char*[Height];
for(unsigned int j = 0; j < Height; ++j)
{
buffer[i][j] = new unsigned char[3];
}
}
FillAndRead(buffer, Width, Height);
}
It …
On this line
r = bufImage[im_x][im_y][0];
it says "error: no match for 'operator[]'
If I make an array like this
GLubyte bufImage[100][100][3];
I can pass bufImage to a function and then get the values using:
r = bufImage[im_x][im_y][0];
g = bufImage[im_x][im_y][1];
b = bufImage[im_x][im_y][2];
However, if I don't know Width and Height at runtime, this does not work. I tried to do this:
GLubyte*** bufImage = new GLubyte**[Width];
for(unsigned int i = 0; i < Width; ++i)
{
bufImage[i] = new GLubyte*[Height];
for(unsigned int j = 0; j < Height; ++j)
{
bufImage[i][j] = new GLubyte[3];
}
}
but it crashes when I try to access them. Can someone help me make this 2d array at runtime?
Thanks,
Dave
I've seen a few questions about this floating around here (mostly from me :) ) but I finally got this is into a nice working form - maybe it will be useful for someone.
It is called like this:
void TestParallelSort()
{
vector<double> Numbers;
Numbers.push_back(3.4);
Numbers.push_back(4.5);
Numbers.push_back(1.2);
vector<string> Names;
Names.push_back("David");
Names.push_back("Hayley");
Names.push_back("Joe");
Tools::ParallelSort(Numbers, Names);
/*
output should be
1.2 Joe
3.4 David
4.5 Hayley
*/
//Tools::OutputVector(Indices);
Tools::OutputVector(Numbers);
Tools::OutputVector(Names);
}
And here is the code:
template <typename T>
struct NumberedItem
{
unsigned int index;
T Item;
};
template <typename T>
bool operator<(NumberedItem<T> NI1, NumberedItem<T> NI2)
{
//return NI1.index < NI2.index;
return NI1.Item < NI2.Item;
}
template <class T, class S>
void ParallelSort(vector<T> &Things1, vector<S> &Things2)
{
//this function sorts Things1 and reorders Things2 so that it is in the same order as the sorted Things1
assert(Things1.size() == Things2.size());
unsigned int NumThings = Things1.size();
//create the sortable objects
vector<NumberedItem<T> > Pairs(NumThings);
for(unsigned int i = 0; i < NumThings; i++)
{
Pairs[i].index = i;
Pairs[i].Item = Things1[i];
}
sort(Pairs.begin(), Pairs.end());
vector<unsigned int> SortedIndices(NumThings);
for(unsigned int i = 0; i < NumThings; i++)
SortedIndices[i] = Pairs[i].index;
vector<T> Things1Out(NumThings);
vector<S> Things2Out(NumThings);
for(unsigned int i = 0; i < NumThings; i++)
{
Things1Out[i] = Pairs[i].Item;
Things2Out[i] = Things2[SortedIndices[i]];
}
//return by reference
Things1 = Things1Out;
Things2 = Things2Out;
}
I don't know how it works with windows, but in linux you have to use some kind of "UI Framework" like QT or GTK.
Even though you are using c++, this is not a c++ question. Ask here:
http://www.opengl.org/discussion_boards/
And I bet you'll get your answer.
They are like arrays, but you can dynamically add items to them using push_back. They also have a function size() to get the length of the vector. I find them amazingly useful!
Throw some code tags around there so it's a bit more readable, but thanks for the tutorial. Is this the right place to post these? I have a handful of things like this that may be useful to others - where should we put them?
It seems like SO often I need to attach a "valid" flag to a standard type. For example - I have a function called "CalculateLunchTime" that I pass a vector of times. If the vector has 0 elements, they did not take a lunch, so the duration is 0 minutes. If the vector has 2 elements, subtract them and that is the lunch duration. If the vector has one element, it doesn't make sense to calculate a duration, but I don't want to return 0 because that was for the first case. In this case the valid range is >0, so of course I could return -1 or something, but it really bothers me having these magic numbers running around. I would really want to do something like
ValidDouble LunchTime(vector<time> &Times)
{
ValidDouble lunchtime;
if(Times.size() == 1)
lunchtime.valid = false;
else
{
lunchtime = Times(1) - Times(0);
lunchtime.valid = true;
}
return lunchtime;
}
Of course I could make a struct with a double and a bool, but I have really never seen anyone else do that so I feel like it could be a bad idea (why?). It would seem a little nicer if I could derive ValidDouble from double. Is that possible?
Thanks,
Dave
I've seen a few posts on here talking about "databases" as if they were just files and using fstream to manipulate the file pointer, etc.
I'm familiar with SQL, so it would be nice to simply be able to execute sql command (as can be done from vb.net) such as:
SELECT * FROM table_name
I googled and found sqlapi++, is that the best way to go?
Thanks,
Dave
You cannot do this, it's simply incorrect syntax:
Furlong(class Kilometer& kilo)
You just need to do
Furlong(Kilometer& kilo)
And also, you really shouldn't/can't have both classes contain the other class. In this case I would make two non-member functions
Furlong KilometerToFurlong(Kilometer &kilo);
Kilometer FurlongToKilometer(Furlong &furl);
Because the conversion functions aren't really helping anything by being member functions.
Hope this helps.
Dave
Ah I see, great!
I have two functions: GetDayTime and GetLunchTime. From some places in the program, all I have is the associate name and a date and I want to determine these quantities. So I just query the database inside the function:
Public Function CalculateLunchTime(ByVal DateToCalculate As DateTime) As Double
Dim daEvent As New TTDataSetTableAdapters.PunchEventTableTableAdapter
Dim EventTable As New TTDataSet.PunchEventTableDataTable
daEvent.Fill(EventTable)
Dim SelectString As String = "associate='" & Name & "' and timefield>=#" & DateToCalculate & "# and timefield <#" & DateToCalculate.AddDays(1) & "# and reason='LUNCH'"
Dim SortString As String = "timeField"
'Return the calculated time
End Function
However, I then also have a "GenerateReport" function which loops through all of the associates, and a big range of dates and calls GetDayTime and GetLunchTime for all of these. An you can imagine, this is quite expensive! (ie VERY slow). Clearly I would want to query the database once to get all the information and then just pass arrays of the information to these two functions. Is it that simple - I just have to make a second version of these functions that takes arrays of the data as input? It just seems like a bad idea to me because of the "code reuse" idea - if a year later I have to make a change, I have to make the same change in two places, which always seems like a bad idea.
Any suggestions/advice?
Thanks,
Dave
But it shouldn't see the end of file until after the 4th line is read, not the third, right?
See this thread about how/why to fix that stringstream problem:
http://www.daniweb.com/forums/post820836.html#post820836
Yep that fixed it - why would it have gone into an error state? I threw this together to answer someone's thread:
void SkipComments(const string &Filename)
{
/* test.txt
P3
# example comment
512 512
255
*/
ifstream fin(Filename.c_str());
if(fin == NULL)
cout << "Cannot open file." << endl;
char MV[2];//the first 2 ascii values
int width, height;
int MPV;//max pixel value
string line;
getline(fin, line); //get first line
MV[0] = line[0];
MV[1] = line[1];
cout << "MV[0] = " << MV[0] << endl;
cout << "MV[1] = " << MV[1] << endl;
line = "";
getline(fin, line); //get second line
string FirstChar = line.substr(0, 1);
if(FirstChar == "#")
cout << "Skipped this line." << endl;
line = "";
getline(fin, line); //get third line
stringstream LineStream;
LineStream << line;
LineStream >> width >> height;
cout << "Width = " << width << endl;
cout << "Height = " << height << endl;
LineStream.str("");
line = "";
getline(fin, line); //get fourth line
LineStream.str("");
LineStream.clear();
LineStream << line;
LineStream >> MPV;
cout << "MPV = " << MPV << endl;
}
It doesn't seem like there should be any problems with that?
Dave
This should do what you're looking to do. For some reason there is a small issue with re-using the string stream for the 4th line (so I made a new one, I left the code that doesn't work commented), but I've started a thread about why that is.
void SkipComments(const string &Filename)
{
/* test.txt
P3
# example comment
512 512
255
*/
ifstream fin(Filename.c_str());
if(fin == NULL)
cout << "Cannot open file." << endl;
char MV[2];//the first 2 ascii values
int width, height;
int MPV;//max pixel value
string line;
getline(fin, line); //get first line
MV[0] = line[0];
MV[1] = line[1];
cout << "MV[0] = " << MV[0] << endl;
cout << "MV[1] = " << MV[1] << endl;
line = "";
getline(fin, line); //get second line
string FirstChar = line.substr(0, 1);
if(FirstChar == "#")
cout << "Skipped this line." << endl;
line = "";
getline(fin, line); //get third line
stringstream LineStream;
LineStream << line;
LineStream >> width >> height;
cout << "Width = " << width << endl;
cout << "Height = " << height << endl;
LineStream.str("");
line = "";
getline(fin, line); //get fourth line
/*
LineStream.str("");
LineStream << line;
LineStream >> MPV;
*/
stringstream LineStream2;
LineStream2 << line;
LineStream2 >> MPV;
cout << "MPV = " << MPV << endl;
/*
//this is how you would read the rest of the lines in the file
vector<string> Lines;
while(getline(fin, line))
{
Lines.push_back(line);
}
*/
}
Dave
If I use the uncommented 3 lines for getting an int out of a stringstream, the value in MPV is wrong. However, if I make a new stringstream, then it works fine. I thought setting .str("") was essentially resetting the stringstream?
line = "";
getline(fin, line); //get fourth line
//these lines don't work
LineStream.str("");
LineStream << line;
LineStream >> MPV;
/*
//these lines do
stringstream LineStream2;
LineStream2 << line;
LineStream2 >> MPV;
*/
Thanks,
Dave
I had this same question:
http://www.daniweb.com/forums/thread114375.html
The answer was basically "you've designed it wrong"!
With text files I don't think there is much option but to read in the whole file, and write out the whole file again, but this time with the new value in the place of the old value. I think you'd have to use an actual database to be able to simply change that one value.
Dave
I got it to work... shouldn't have been this difficult to figure out.
The key was to use the filter() method of the binding source, rather than setting the DataGridView datasource to the select() method of the datatable.
PunchEventTableBindingSource.Filter = FilterString
PunchEventTableBindingSource.Sort = SortString
Then I can delete a row like this:
DirectCast(PunchEventTableBindingSource.Current, DataRowView).Delete()
I really never use arrays, I go for the vector of vectors
vector<vector<char> > array(columns);
for(unsigned int i = 0; i < columns; i++)
{
vector<char> RowVector(rows);
// ... fill the row vector ...
array.push_back(RowVector);
}
If you want to use an actually array you have to use "new", because as it's telling you, you aren't allowed to determine the size of an array declared like you were doing it at runtime.
Any time there is an "if" I'd say it would either turn into a piecewise function or you'd need some wacky step functions.
Also, please use code tags.
I do it just to remind myself (and the compiler) that no class variables can be changed in this function. Basically just to keep me from accidentally doing something stupid!
ie.
double YourClass::GetX()
{
x_ = 2.0;
}
You should not be modifying any of the member variables in a get function! But the compiler doesn't know that, unless you do this:
double YourClass::GetX() const
{
x_ = 2.0;
}
Then it will give you an error, because you are not allowed to modify x_ from this function!
Hope that helps,
Dave
So you can actually change the row index of a row in a table?
I mentioned I tried calling acceptchanges after deleting the row with no change, which does seem odd?
I have a delete button which does this:
TTDataSet.PunchEventTable.Rows(dgvData.SelectedRows(0).Index).Delete()
The problem is that if I delete row 0 (the 0th index in the DataGridView and in the underlying table, all is well. But now row 1 in the table corresponds to row 0 in the DataGridView, so if I run that code to delete a row again, nothing happens because row 0 in the table is already deleted.
I tried
PunchEventTableBindingSource.EndEdit()
and
TTDataSet.PunchEventTable.AcceptChanges()
after the call to delete, but neither seems to fix the problem. Of course the problem is fixed once Update is called on the DataAdapter, but I didn't want to do that until all of the changes were made, this way I could cancel the changes if the user so wishes.
Is there a way to fix this?
Thanks,
Dave
Ok so it seems you have to put the "generic" template function in the h file:
class.h
--------
template <typename T>
void Output(vector<T> &V)
{
cout << endl << "other" << endl << "-------" << endl;
for(unsigned int i = 0; i < V.size(); i++)
cout << V[i] << endl;
}
and the specialization in the cpp file
class.cpp
--------
template <>
void Output<double>(vector<double> &V)
{
cout << endl << "double" << endl << "----------" << endl;
for(unsigned int i = 0; i < V.size(); i++)
cout << V[i] << endl;
}
template <>
void Output<unsigned int>(vector<unsigned int> &V)
{
cout << endl << "unsigned int" << endl << "----------" << endl;
for(unsigned int i = 0; i < V.size(); i++)
cout << V[i] << endl;
}
That look reasonable to everyone? It seems to work.
Thanks,
Dave
hmm what I had found before was I had to leave all the implementations in the .h file or it wouldn't work... but here he has put them in the cpp file with only a declaration in the .h file... I'll try it out.
Do you know what I'm talking about with this "partial specialization" terminology?
I have tried to make a SumVector function for all types, but if it is called with a vector of unsigned char's, it will do something special. Here is what I tried, but I get a multiple definitions of the function compiler error.
template <typename T>
void SumVector(vector<T> &V)
{
T Sum = 0;
for(unsigned int i = 0; i < V.size(); i++)
Sum += V[i];
cout << "Sum: " << Sum << endl;
}
template <>
void SumVector<unsigned int>(vector<unsigned int> &V)
{
unsigned int Sum = 0;
for(unsigned int i = 0; i < V.size(); i++)
Sum += V[i];
cout << "Unsigned int sum: " << Sum << endl;
}
Also, am I right that this is called "partial template specialization"? I saw a bunch of tutorials but I couldn't gather exactly what normal "template specialization" was, it looked they were just defining a separate function for each type, just like overloading?
Thanks,
Dave
What have you tried?
I'm not sure how to do this, but looks like this is what you want:
http://www.devx.com/vb2themax/Tip/18773
I've found boost particularly hard to work with, especially if you don't know what you're doing!
It sounds like you just want a switch statement?
http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Companion/cxx_crib/switch.html
If you have a more exact goal then let us know.
We can't help you with 100 errors, especially if we don't know what they are. You need to narrow down the problem to < 20 lines of code and ask a specific question.
I have a program with about 20 forms, almost all of which use the data from a tables in a dataset (from a datasource I added to the project). So I have made a global variable in a module:
Public TTData As New TTDataSet
And a DataAdapter for each table (also global):
Public daEvent As New TTDataSet.EventTableAdapter
And then I just use it from the forms when it is needed, ie
Dim OldRow As TTDataSet.EventTableRow = TTData.EventTable.Rows(0)
This works fine (although is there a better way, ie without globals?)
On one of these forms I want to display some data in a DataGridView. If I just drag the datatable from the project datasources onto the form, it creates a DataSet, DataAdapter, and BindingSource on the form. This is now a SECOND copy of the data, because I already have it in a global variable. I'd have to either reload all of the globals after editing anything in the local copy in the datagridview, or do this a different way.
Did I make the problem clear? Does anyone have any suggestions?
Thanks,
Dave
Which line do you get the pointer comparison error?
In this code:
int random_integer = (rand()%6)+1;
random_integer = pcdice1;
You are storing the random number in random_integer, and then immediately replacing it with pcdice1. That is why you aren't seeing a different number each time.
Dave
You're map is supposed to store a string and a ConfigPaths* , but you are trying to pass it s1 (a string) and m1 (a ConfigPath, NOT a ConfigPath*)
I've never used that ::value_type, so I could be wrong, but thats the first thing I'd look at.
Yea, but it looks like you're using it on line 77 and defining it on line 241 - you need to define it before you use it!
I believe this is generally called tokenizing? Take a look at this -
http://www.daniweb.com/forums/thread27905.html
Is that what you were asking?
I have a typed datatable that I am viewing part of with a datagridview
dgvData.DataSource = TTData.PunchEventTable.Select(FilterString, SortString)
My question is how do I delete the row that is selected in the DataGridView from the database? If I use
TTData.PunchEventTable.Rows.RemoveAt(dgvData.SelectedRows(0).Index)
I believe it will delete the wrong row!
Any suggestions?
Thanks,
Dave
I don't understand the relationship between truck_data and position_axle
Maybe give a sample input/output?
You seem to have not tried at all. Give it a shot and then if it doesn't work we'll help you.