Howard <al*****@hotmail.com> wrote:
"Marcus Kwok" <ri******@gehennom.net> wrote in message
news:dg**********@news-int2.gatech.edu... The compiler gives me a "signed/unsigned mismatch" warning (I am using
the C++ compiler found in Visual Studio .NET 2003, which tells me that
the return type is a size_t).
Any reason you're not using unsigned int for i?
(You can also feel free to ignore the warning, if you know it'll never be a
real problem.)
I listed some reasons in the original post (wrap-around and implicit
conversion issues and gave example code from Stroustrup). I mean, I
know MY code will never make that mistake ;-) but I try to code
defensively when possible. In either case, even if I declare it as an
unsigned, it still warns me that there is a "conversion from size_t to
unsigned int, possible loss of data". Now, I know that the chance of
losing data converting from a size_t (an unsigned type) to an unsigned
int is pretty small, but I like clean compiles with no warnings (please
don't tell me to just disable this warning message :) ). I do not think
using a size_t would be appropriate either, as I am using i for another
purpose.
OK, maybe it will help if I post the ACTUAL code (the code I gave was an
extremely simplified version), and then possibly someone can see a
better way to do it that will avoid the issue. I will simplify less,
and add comments.
The int Xmtr.num is being used to store the index of the transmitter.
Then, we check a vector of receivers to see if we have the data for this
transmitter, and if not then we add a new entry for it, with the same
index. Let me know if this is not clear after reading the code.
void FSBSModel::fsbs_set(const FSBSParameters& params)
{
// ...
// params.xmtrs is a std::vector<Xmtr_setq>
// xmtrs is a std::vector<Xmtr>
typedef std::vector<Xmtr_setq>::const_iterator XCI;
XCI xend = params.xmtrs.end();
// for each Xmtr_setq passed in through params,
// add it to the list of real xmtrs
for (XCI xci = params.xmtrs.begin(); xci != xend; ++xci) {
int num_xmtrs = static_cast<int>(xmtrs.size());
Xmtr toAdd;
toAdd.name = xci->name;
toAdd.num = num_xmtrs;
// xmtr_fill() will fill in the rest of the Xmtr based on its name
xmtr_fill(toAdd);
xmtrs.push_back(toAdd);
}
}
void FSBSModel::read_prop_files()
{
// ...
std::vector<Xmtr>::const_iterator xci;
std::vector<Rcvr>::iterator ri;
// ...
int xmtr_i = xci->num;
// ri->signal is a std::vector<Matrix> that holds the signal info
// for a specific transmitter (ri->signal[xmtr_i]) at that receiver
// location
// if the below condition is true, then this receiver does not have
// any signal info for that transmitter, so we add a blank Matrix
// (it will be filled later)
if (xmtr_i >= static_cast<int>(ri->signal.size())) {
Matrix temp999;
// ...
ri->signal.push_back(temp999);
}
}
A somewhat related but tangential question, can anyone see a better
implementation for this function?
// setcol() sets the elements of the colv vector
// to the individual phrases found in input_record, which is tab-delimited.
//
// The column numbers start at 0.
//
// The number of columns found is returned.
int setcol(const std::string& input_record, std::vector<std::string>& colv)
{
std::string temp;
colv.clear();
typedef std::string::const_iterator CI;
CI end = input_record.end();
for (CI ci = input_record.begin(); ci != end; ++ci) {
if (*ci != '\t') {
temp += *ci;
}
else {
colv.push_back(temp);
temp.clear();
}
}
colv.push_back(temp);
return static_cast<int>(colv.size());
}
Originally I implemented it like the below, which I feel is much more
elegant, but I could not figure out how to get it to separate the
phrases only on tabs; it would delimit with any whitespace.
int setcol(const std::string& input_record, std::vector<std::string>& colv)
{
std::istringstream s(input_record);
std::string temp;
colv.clear();
while (s >> temp)
{
colv.push_back(temp);
}
return colv.size();
}
Thanks in advance.
--
Marcus Kwok