 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Marc Ouellette Guest
|
Posted: Thu Aug 18, 2005 7:20 pm Post subject: Weird STL Problem |
|
|
Hi Everyone,
I have a really weird problem.
Here is the code
Tmap::const_iterator i= main.find(tableHour);
if (i == main.end()) throw CFfmcHourlyException("Bad table hour");
unsigned indexTime = i->second;
assert(indexTime >= 1 && indexTime <= 20);
On the first line of the code I always get and EAccessViolation.
The really weird part is that this use to work just fine and I had to make
changes in other cpp files that have nothing to do with this one. So I am
not really sure where to look. Any ideas?
Thanks,
Marc
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Thu Aug 18, 2005 7:42 pm Post subject: Re: Weird STL Problem |
|
|
"Marc Ouellette" <admin (AT) scp-solutions (DOT) com> wrote
| Quote: | On the first line of the code I always get and EAccessViolation.
|
You did not show what TMap, main, or tableHour are declared asl, or where
this code is being called from. There is not enough information to diagnose
your problem. Please provide a code snippet that actually compiles.
Gambit
|
|
| Back to top |
|
 |
Alisdair Meredith [TeamB] Guest
|
Posted: Fri Aug 19, 2005 8:46 am Post subject: Re: Weird STL Problem |
|
|
Marc Ouellette wrote:
| Quote: | Tmap::const_iterator i= main.find(tableHour);
^^^^ |
| Quote: | On the first line of the code I always get and EAccessViolation.
The really weird part is that this use to work just fine and I had to
make changes in other cpp files that have nothing to do with this
one. So I am not really sure where to look. Any ideas?
|
Does this still happen if you change the name of your object?
main is the name of a function you will find in most applications, and
re-using identifiers is a good source of confusion, even if it is legal!
And anything that can confuse us, can also fool compilers, so best to
avoid such issues where unnecessary.
AlisdairM(TeamB)
|
|
| Back to top |
|
 |
Marc Ouellette Guest
|
Posted: Fri Aug 19, 2005 12:41 pm Post subject: Re: Weird STL Problem |
|
|
Sorry here is the full class implementation. I have identified where the
error happens.
It gets declared in another class
// singleton class to calculate hourly ffmc values
CFfmcHourly ffmcHourly;
Values are filled in by the following function and it returns true.
bool Weather::ReadFwiFiles()
{
using Parameter::dirWeatherData;
if ( !ffmcHourly.Read(File::Join(dirWeatherData, "diurnalFFMC1.dat")) )
return false;
return true;
}
then used here and the error happends in the get function.
If anyone has any ideas please let me know. I also renamed the main Tmap to
be called low.
localFwiWind.sngFFMC = ffmcHourly.Get(sngCellFFMC, intTableHour);
//Header
// File: ffmcHourly.h
// Pupose: Declares class that manages the high and main hourly ffmc
tables.
// Class provides members to read the tables from file and to
// interpolate hourly ffmc values.
#ifndef ffmcHourlyH
#define ffmcHourlyH
#include <string>
#include <map>
// exception class for CFfmcHourly
class CFfmcHourlyException : public std::exception
{
public:
CFfmcHourlyException(const std::string& message) : _message(message){}
virtual const char* what() const throw() { return _message.c_str(); }
private:
std::string _message;
};
// class to store the high and main ffmc hourly tables, and to
// interpolate hourly ffmc values
class CFfmcHourly
{
typedef unsigned TtableHour;
typedef unsigned Tindex;
typedef std::map<TtableHour, Tindex> Tmap;
// map used to store the link between table hour and index into
// the hourly ffmc tables (e.g. 600->1, 700->2, etc...)
public:
CFfmcHourly();
// constructs the high and main table hour-to-index maps
bool Read (const std::string& filename);
// reads the table of hourly ffmc values from file
float Get (float FF_FFMC, int tableHour) const
throw(CFfmcHourlyException);
// returns the hourly FFMC given the base FFMC and table hour
private:
float gSngH[9][39];
float gSngMAIN[22][39];
Tmap high; // link between high table hour and index
Tmap low; // link between main table hour and index
};
#endif
//end of header
//CPP File
// File: ffmcHourly.cpp
#include <fstream>
#include <cstdlib>
#include "ffmcHourly.h"
#include <assert>
using std::atof;
CFfmcHourly::CFfmcHourly ()
{
// initialize the high table hour-to-index map
// t = table hour, i = index into data array
for(unsigned t = 600, i = 1; i <= 6; t+=100, i++) {
high[t] = i;
}
high[1159] = 7;
// initialize the main table hour-to-index map
for(unsigned t = 100, i = 1; i <= 5; t+=100, i++) {
low[t] = i;
}
low[559] = 6;
for(unsigned t = 1200, i = 8; i <= 20; t+=100, i++) {
low[t] = i;
}
}
bool CFfmcHourly::Read (const std::string& filename)
{
std::ifstream file(filename.c_str());
if (!file.is_open()) return false;
const short recordSize = 300;
char buffer[recordSize + 1];
// populate the gSngH array
file.getline(buffer, recordSize); // skip two header lines
file.getline(buffer, recordSize);
for(int indexTime = 0; indexTime <= 8; indexTime++){
file.getline(buffer, recordSize);
gSngH[indexTime][0] = atof(strtok(buffer,","));
for(int indexFFMC = 1; indexFFMC <= 38; indexFFMC++){
gSngH[indexTime][indexFFMC] = atof(strtok(NULL,","));
}
}
// populate the gSngMain array
file.getline(buffer, recordSize); // skip header line
for(int indexTime = 0; indexTime <= 21; indexTime++){
file.getline(buffer, recordSize);
gSngMAIN[indexTime][0] = atof(strtok(buffer,","));
for(int indexFFMC = 1; indexFFMC <= 38; indexFFMC++){
gSngMAIN[indexTime][indexFFMC] = atof(strtok(NULL,","));
}
}
return true;
}
// returns the hourly FFMC given the base FFMC and hour
float
CFfmcHourly::Get (float FF_FFMC, int tableHour) const
throw(CFfmcHourlyException)
{
// check range of FF_FFMC
if (FF_FFMC < 17.5) {
FF_FFMC = 17.5;
}
else if (FF_FFMC > 100.9) {
FF_FFMC = 100.9;
}
float adjFFMC;
if(tableHour >= 600 && tableHour <= 1159){
Tmap::const_iterator i = high.find(tableHour);
if (i == high.end()) throw CFfmcHourlyException("Bad table hour");
unsigned indexTime = i->second;
assert(indexTime >= 1 && indexTime <= 7);
// interpolate the FFMC values in the table; first find index of
// column with FFMC less than FF_FFMC
int indexFFMC = 1;
while(FF_FFMC >= gSngH[0][indexFFMC]) {
indexFFMC += 1;
}
indexFFMC -= 1; // get preceding column
// calculate fraction that FF_FFMC is between the two columns
float fractionFFMC = (FF_FFMC - gSngH[0][indexFFMC]) /
(gSngH[0][indexFFMC+1] - gSngH[0][indexFFMC]);
// interpolate the FFMC value using fractionFFMC
adjFFMC = gSngH[indexTime][indexFFMC] +
(gSngH[indexTime][indexFFMC+1] - gSngH[indexTime][indexFFMC]) *
fractionFFMC;
}
else {
if (tableHour < 100) {
tableHour += 2400;
}
//////error happens on the next line /////////
Tmap::const_iterator i= low.find(tableHour);
if (i == low.end()) throw CFfmcHourlyException("Bad table hour");
unsigned indexTime = i->second;
assert(indexTime >= 1 && indexTime <= 20);
// interpolate the FFMC values in the table
int indexFFMC = 1;
while(FF_FFMC >= gSngMAIN[0][indexFFMC]) {
indexFFMC += 1;
}
indexFFMC -= 1;
// calculate fraction that FF_FFMC is between the two columns
float fractionFFMC = (FF_FFMC - gSngMAIN[0][indexFFMC]) /
(gSngMAIN[0][indexFFMC+1] - gSngMAIN[0][indexFFMC]);
// interpolate the FFMC value using fractionFFMC
adjFFMC = gSngMAIN[indexTime][indexFFMC] +
(gSngMAIN[indexTime][indexFFMC+1] - gSngMAIN[indexTime][indexFFMC]) *
fractionFFMC;
}
if(adjFFMC < 0.0)adjFFMC = 0.0;
else if(adjFFMC > 101.0)adjFFMC = 101.0;
return adjFFMC;
}
//
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|