Compile and run libconfig++ [duplicate] - c++

This question already has answers here:
Boost::system link error on Ubuntu
(2 answers)
Closed 9 years ago.
I installed:
sudo apt-get install libconfig++-dev
After that I want compile and run the example program libconfig++.
Program:
/* ----------------------------------------------------------------------------
libconfig - A library for processing structured configuration files
Copyright (C) 2005-2010 Mark A Lindner
This file is part of libconfig.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 2.1 of
the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, see
<http://www.gnu.org/licenses/>.
----------------------------------------------------------------------------
*/
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <libconfig.h++>
using namespace std;
using namespace libconfig;
// This example reads the configuration file 'example.cfg' and displays
// some of its contents.
int main(int argc, char **argv)
{
Config cfg;
// Read the file. If there is an error, report it and exit.
try
{
cfg.readFile("example.cfg");
}
catch(const FileIOException &fioex)
{
std::cerr << "I/O error while reading file." << std::endl;
return(EXIT_FAILURE);
}
catch(const ParseException &pex)
{
std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
<< " - " << pex.getError() << std::endl;
return(EXIT_FAILURE);
}
// Get the store name.
try
{
string name = cfg.lookup("name");
cout << "Store name: " << name << endl << endl;
}
catch(const SettingNotFoundException &nfex)
{
cerr << "No 'name' setting in configuration file." << endl;
}
const Setting& root = cfg.getRoot();
// Output a list of all books in the inventory.
try
{
const Setting &books = root["inventory"]["books"];
int count = books.getLength();
cout << setw(30) << left << "TITLE" << " "
<< setw(30) << left << "AUTHOR" << " "
<< setw(6) << left << "PRICE" << " "
<< "QTY"
<< endl;
for(int i = 0; i < count; ++i)
{
const Setting &book = books[i];
// Only output the record if all of the expected fields are present.
string title, author;
double price;
int qty;
if(!(book.lookupValue("title", title)
&& book.lookupValue("author", author)
&& book.lookupValue("price", price)
&& book.lookupValue("qty", qty)))
continue;
cout << setw(30) << left << title << " "
<< setw(30) << left << author << " "
<< '$' << setw(6) << right << price << " "
<< qty
<< endl;
}
cout << endl;
}
catch(const SettingNotFoundException &nfex)
{
// Ignore.
}
// Output a list of all books in the inventory.
try
{
const Setting &movies = root["inventory"]["movies"];
int count = movies.getLength();
cout << setw(30) << left << "TITLE" << " "
<< setw(10) << left << "MEDIA" << " "
<< setw(6) << left << "PRICE" << " "
<< "QTY"
<< endl;
for(int i = 0; i < count; ++i)
{
const Setting &movie = movies[i];
// Only output the record if all of the expected fields are present.
string title, media;
double price;
int qty;
if(!(movie.lookupValue("title", title)
&& movie.lookupValue("media", media)
&& movie.lookupValue("price", price)
&& movie.lookupValue("qty", qty)))
continue;
cout << setw(30) << left << title << " "
<< setw(10) << left << media << " "
<< '$' << setw(6) << right << price << " "
<< qty
<< endl;
}
cout << endl;
}
catch(const SettingNotFoundException &nfex)
{
// Ignore.
}
return(EXIT_SUCCESS);
}
// eof
Example config file:
// An example configuration file that stores information about a store.
// Basic store information:
name = "Books, Movies & More";
// Store inventory:
inventory =
{
books = ( { title = "Treasure Island";
author = "Robert Louis Stevenson";
price = 29.99;
qty = 5; },
{ title = "Snow Crash";
author = "Neal Stephenson";
price = 9.99;
qty = 8; }
);
movies = ( { title = "Brazil";
media = "DVD";
price = 19.99;
qty = 11; },
{ title = "The City of Lost Children";
media = "DVD";
price = 18.99;
qty = 5; },
{ title = "Memento";
media = "Blu-Ray";
price = 24.99;
qty = 20;
},
{ title = "Howard the Duck"; }
);
};
// Store hours:
hours =
{
mon = { open = 9; close = 18; };
tue = { open = 9; close = 18; };
wed = { open = 9; close = 18; };
thu = { open = 9; close = 18; };
fri = { open = 9; close = 20; };
sat = { open = 9; close = 20; };
sun = { open = 11; close = 16; };
};
I`m trying e.g.:
g++ -lconfig++ example1.cpp -o example1
Output:
/tmp/ccOI0efx.o: In function `main':
example1.cpp:(.text+0x21): undefined reference to `libconfig::Config::Config()' (..)
How to compile this program? What is necessary command?
Another way:
I also downloaded package:
libconfig-1.4.9.tar.gz
extract and done "Install" file, I mean:
The simplest way to compile this package is:
cd' to the directory containing the package's source code and type
./configure'...
Type `make' to compile the package.
Optionally, type `make check'...
Type `make install' to install the programs and any data files and
documentation.
You can remove the program binaries and object files from the
source code directory by typing `make clean'...
After that try to run c++ example
(cd examples/c++ and make) nothing to do?
Output:
make: *** No rule to make target `../../lib/libconfig++.la', required by `example1'. Stop.

Run this command :
g++ example1.cpp -lconfig++ -Wall -o example1 && ./example1

Related

cargo transportation system we are not sure how to display the last part of our task

Here is our code for the task we are almost finishing just the last part we are stuck at
"Fastest: 3 trips (1 Van, 3 Mini-lorry, $645) "
we are not sure how to display the values in the bracket we only able to display 3 trips.
Is there a way to also display the values in the bracket stated as well?
we use
int min = *min_element(vTrips.begin(), vTrips.end());
cout << "Fastest: " << min << " trips" << endl;
but this only display the 3 trips.
#include <iostream>
#include <vector>
#include <iterator>
#include <fstream>
#include<algorithm>
using namespace std;
class CTS //cargo transport system
{
int i;
int cargo, lorryprice, vanprice, lorrysize, vansize, allOps;
public:
void set_cargo(int);
void set_lorryprice(int);
void set_vanprice(int);
void set_lorrysize(int);
void set_vansize(int);
};
void CTS::set_cargo(int total_cargo) {
cargo = total_cargo;
}
void CTS::set_lorryprice(int lorryP) {
lorryprice = lorryP;
}
void CTS::set_vanprice(int vanP) {
vanprice = vanP;
}
void CTS::set_lorrysize(int lorryS) {
lorrysize = lorryS;
}
void CTS::set_vansize(int vanS)
{
vansize = vanS;
}
int main()
{
int cargo, lorryprice, vanprice, lorrysize, vansize, options, i, no_lorry, no_van, cost, trips;
ifstream infile;
infile.open("size.txt");
if (infile.is_open()) {
infile >> cargo;
infile >> lorryprice;
infile >> vanprice;
infile >> lorrysize;
infile >> vansize;
}
CTS run;
run.set_cargo(cargo);
run.set_lorryprice(lorryprice);
run.set_vanprice(vanprice);
run.set_lorrysize(lorrysize);
run.set_vansize(vansize);
infile.close();
options = (cargo / lorrysize) + 1;
no_lorry = (cargo / lorrysize);
no_van = (cargo / vansize) + 3;
if (cargo % lorrysize == 0) {
no_van = -3;
}
if (cargo % lorrysize != 0) {
no_van = ((cargo % lorrysize) / 10) - 3;
}
/*it = numbervan.begin();
for (auto ir = numbervan.rbegin(); ir != numbervan.rend(); ++ir) {
cout << *ir << endl;
}*/
vector<int> vCost, vVan, vTrips, vLorry;
vector <int>::iterator it;
for (i = 1; i < options + 1; i++)
{
int numberlorry = no_lorry;
cout << "Option " << i << ":" << endl;
cout << "Number of Mini-Lorries : " << no_lorry-- << endl;
if (no_van >= -3) {
no_van += 3;
}
cout << "Number of Vans : " << no_van << endl;
int numbervan = no_van;
if (numberlorry > numbervan) {
trips = numberlorry;
}
else {
trips = numbervan;
}
cout << "Trips Needed : " << trips << endl;
cost = (numberlorry * lorryprice) + (no_van * vanprice);
cout << "Total Cost : $" << cost << endl;
vCost.push_back(cost);
vLorry.push_back(numberlorry);
vVan.push_back(numbervan);
vTrips.push_back(trips);
}
int counter = vCost.size() - 1;
//std::vector<int>::reverse_iterator ir = vCost.rbegin();
for (i = 1; i < 4; i++) {
//cout << "Lowest #" << i << ": "<<cost<<endl;
cout << "Lowest #" << i << ": $" << vCost[counter] << "(" << vVan[counter] << " Vans, " << vLorry[counter] << " Mini-Lorry, " << vTrips[counter] << " Trips)" << endl;
counter--;
}
int min = *min_element(vTrips.begin(), vTrips.end()); // this line of code we figured out how to
cout << "Fastest: " << min << " trips" << endl; //display the number of trips using algorithm
return 0;
}
Your design is awkward; you create an instance of CTS run; and never use it.
Assuming that you do your calculations right, you need to know at what index you found min. If you store the iterator returned by min_element(), you can get an index by subtracting vTrips.begin() from it. Then the corresponding elements in your vCost, vLorry and vVan vectors will contain the data you want.
However, it would be easier if you define a struct containing your pre-calculated values, and push that into some vector. In that case, all related data is kept together.

How to ignore certain input lines in C++?

Okay so a little background this code is supposed to read through a file containing DNA and calculate the number of nucleotides A, C, T, G and print them out and also do some other slight calculations. My code runs fine for most files except for files that contain lines that start with # and + in the file. I need to skip those lines in order to get an accurate number. So my question is how to skip or ignore these lines in my calculations.
My code is
#include <iostream>
#include <stream>
#include <string>
#include <vector>
#include <map>
int main(int argc, char** argv) {
// Ignore how the above argc and argv are used here
auto arguments = std::vector<std::string>(argv, argv + argc);
// "arguments" box has what you wrote on the right side after &&
if (arguments.size() != 2) {
// ensure you wrote a file name after "./a.out"
std::cout << "Please give a file name as argument\n";
return 1;
}
auto file = std::fstream(arguments[1]);
if (!file) {
// ensure the file name you gave is from the available files
std::cout << "Cannot open " << arguments[1] << "\n";
return 1;
}
auto counts = std::map<char,int>({{'G',0.0},{'A',0.0},{'C',0.0},{'T',0.0}});
// Just a test loop to print all lines from the file
for (auto dna = std::string(); std::getline(file, dna); ) {
//std::cout << dna << "\n";
for (auto nucleotide:dna) {
counts[nucleotide]=counts[nucleotide] + 1;
}
}
double total = counts['A'] + counts['T'] + counts['G'] + counts['C'];
double GC = (counts['G'] + counts['C'])*100/total;
double AT = (counts['A'] + counts['T'])*100/total;
double ratio = AT/GC;
auto classification = "";
if ( 40.0 < GC < 60.0) {
classification = "moderate GC content";
}
if (60 <= GC) {
classification = "high GC content";
}
if (GC <= 40.0) {
classification = "low GC content";
}
std::cout << "GC-content: " << GC << "\n";
std::cout << "AT-content: " << AT << "\n";
std::cout << "G count: " << counts['G'] << "\n";
std::cout << "C count: " << counts['C'] << "\n";
std::cout << "A count: " << counts['A'] << "\n";
std::cout << "T count: " << counts['T'] << "\n";
std::cout << "Total count: " << total << "\n";
std::cout << "AT/GC Ratio: " << ratio << "\n";
std::cout << "GC Classification: " << classification << "\n";
}
The file that is giving me trouble is this which is like this
#ERR034677.1 HWI-EAS349_0046:7:1:2144:972#0 length=76
NGATGATAAACAAGAGGGTAAAAAGAAAAAAGCTACAGACATTTCTGCTAATCTATTATTTTGTTCCTTTTTTTTT
+ERR034677.1 HWI-EAS349_0046:7:1:2144:972#0 length=76
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
If anyone can help me with this. I will be very grateful. I only need a hint or an idea of the concept I am missing so I can make my code compatible with all files. Thanks in advance
Your actual problem seems to be the standard case of "input is not always clean syntax".
The solution is always "do not expect clean syntax".
First read whole lines into a buffer.
Then check for syntax.
Skip broken syntax.
Scan clean syntax from buffer.

error C2228: left of '.printStats' must have class/struct/union

So I recently decided to pick up programming again and went with C++. Tried to make an adventurer class, but I seem to be running into some trouble. Here are my files:
Adventurer.h:
#ifndef __Adventurer_H_INCLUDED__ //if Adventurer.h hasn't been included yet...
#define __Adventurer_H_INCLUDED__ //#define this so the compiler knows it has been included
class Adventurer
{
private:
int hp, mp, str, agi, magic, armour;
public:
Adventurer(){}
void printStats();
}
#endif
Adventurer.cpp:
#include <iostream>
#include "Adventurer.h"
Adventurer::Adventurer()
{
hp = 50;
mp = 25;
str = 5;
agi = 5;
magic = 5;
armour = 5;
}
void Adventurer::printStats()
{
cout << "HP = " << hp << "\n\n";
cout << "MP = " << mp << "\n\n";
cout << "str = " << str << "\n\n";
cout << "agi = " << agi << "\n\n";
cout << "magic = " << magic << "\n\n";
cout << "armour = " << armour << "\n\n";
}
RPG_Game.cpp:
// my first program in C++
#include <iostream>
#include <string>
#include "Adventurer.h"
;using namespace std;
int main()
{
cout << "Hello Adventurer! What is your name? \n";
string advName;
cin >> advName;
cout << "\nYour name is " << advName << "!";
Adventurer *adv = new Adventurer();
cout << adv.printStats();
delete adv;
system(pause);
}
Let's look at the errors in your code
First, in your Adventurer.h, put a semicolon (;) after the class.
Next, in that same class, you have
Adventurer(){}
change this to
Adventurer();
Then, in your RPG_Game.cpp , change
cout << adv.printStats();
to
adv->printStats() ;
When using pointers, you need to use -> and not .
And lastly,
system(pause);
should be
system( "pause" );
Now, try running your code.
Also, you might find this helpful.

Bloomberg Equity Option Chain through API

I am currently working on a project that should help me create an implied volatility surface for a given stock. For this purpose, I am writing a script that will download all the available options for this specific stock - from what I've gathered, this is possible by sending a request through the Bloomberg API using bulks fields/overrides. Here is my current code:
d_host = "localhost";
d_port = 8194;
SessionOptions sessionOptions;
sessionOptions.setServerHost(d_host.c_str());
sessionOptions.setServerPort(d_port);
Session session(sessionOptions);
Service refDataService = session.getService("//blp/refdata");
Request request = refDataService.createRequest("ReferenceDataRequest");
request.append("securities", "MSFT US EQUITY");
request.append("fields", "CHAIN_TICKERS");
// add overrides
Element overrides = request.getElement("overrides");
Element override1 = overrides.appendElement();
override1.setElement("fieldId", "CHAIN_PUT_CALL_TYPE_OVRD");
override1.setElement("value", "C");
Element override2 = overrides.appendElement();
override2.setElement("fieldId", "CHAIN_POINTS_OVRD");
override2.setElement("value", 100);
Element override3 = overrides.appendElement();
override3.setElement("fieldId", "CHAIN_EXP_DT_OVRD");
override3.setElement("value", "20250203");
std::cout << "Sending Request: " << request << std::endl;
CorrelationId cid(this);
session.sendRequest(request, cid);
(followed by event handling)
Now I have several issues/questions:
The code compiles without problems, but when running it on the Bloomberg terminal,the following error is printed:
How would I go about fixing this problem? I assume I made a mistake somewhere in the override fields..
How would I need to adjust my code to download all options available given a specific maturity, i.e. I want to get a list of all the options until today + 15 years.
How would I then download the implied volatility for each option? Would I need to store the tickers in an array and then send a request for the field "IVOL_MID" for each option or is there some kind of way to obtain all the volatilities at once?
Edit: Here is the code of my event handler, since that seems to be the problem.
session.sendRequest(request, cid);
while (true)
{
Event event = session.nextEvent();
MessageIterator msgIter(event);
while (msgIter.next()) {
Message msg = msgIter.message();
if (msg.correlationId() == cid) {
processMessage(msg);
}
}
if (event.eventType() == Event::RESPONSE) {
break;
}
}
void processMessage(Message &msg)
{
Element securityDataArray = msg.getElement(SECURITY_DATA);
int numSecurities = securityDataArray.numValues();
for (int i = 0; i < numSecurities; ++i) {
Element securityData = securityDataArray.getValueAsElement(i);
std::cout << securityData.getElementAsString(SECURITY)
<< std::endl;
const Element fieldData = securityData.getElement(FIELD_DATA);
for (size_t j = 0; j < fieldData.numElements(); ++j) {
Element field = fieldData.getElement(j);
if (!field.isValid()) {
std::cout << field.name() << " is NULL." << std::endl;
}
else {
std::cout << field.name() << " = "
<< field.getValueAsString() << std::endl;
}
}
Element fieldExceptionArray =
securityData.getElement(FIELD_EXCEPTIONS);
for (size_t k = 0; k < fieldExceptionArray.numValues(); ++k) {
Element fieldException =
fieldExceptionArray.getValueAsElement(k);
std::cout <<
fieldException.getElement(ERROR_INFO).getElementAsString(
"category")
<< ": " << fieldException.getElementAsString(FIELD_ID);
}
std::cout << std::endl;
}
The problem is in the event handling code that you are not showing. You are probably parsing it incorrectly.
Running your query I get the following result:
MSFT US 01/20/17 C23
MSFT US 01/20/17 C25
MSFT US 01/20/17 C30
MSFT US 01/20/17 C33
MSFT US 01/20/17 C35
MSFT US 01/20/17 C38
MSFT US 01/20/17 C40
MSFT US 01/20/17 C43
MSFT US 01/20/17 C45
MSFT US 01/20/17 C47
MSFT US 01/20/17 C50
MSFT US 01/20/17 C52.5
MSFT US 01/20/17 C55
MSFT US 01/20/17 C57.5
MSFT US 01/20/17 C60
MSFT US 01/20/17 C65
MSFT US 01/20/17 C70
Note: I'm using the Java API but it is essentially the same.
UPDATE:
your code does not parse the field data array element properly: the returned data contains an array of sequences so you need to parse it in two steps. Instead of field.getValueAsString(), you should have a code that looks like this (it's in Java and not tested):
//...
for (int i = 0; i < field.numValues(); i++) {
Element sequence = field.getValueAsElement(i);
ElementIterator it = sequence.elementIterator();
while (it.hasNext()) {
Element e = it.next();
System.out.println(e.getValueAsString());
}
If that does not work I suggest you debug your code step by step and inspect the type of data you receive and handle it accordingly.
For more details you should read the Developer's guide, in particular A.2.3.
As seen in the other answer, the problem lies in the event handling so I've rewritten that part using some examples from the Bloomberg API emulator.
session.sendRequest(request, cid);
bool continueToLoop = true;
while (continueToLoop)
{
Event evt = session.nextEvent();
switch (evt.eventType())
{
case Event::RESPONSE:
continueToLoop = false; //fall through
case Event::PARTIAL_RESPONSE:
ProcessReferenceDataEvent(evt);
break;
}
}
void ProcessReferenceDataEvent(Event evt)
{
const string level1 = "";
const string level2 = "\t";
const string level3 = "\t\t";
const string level4 = "\t\t\t";
std::cout << endl << endl;
std::cout << level1 << "EventType = " << evt.eventType();
MessageIterator iter(evt);
while (iter.next())
{
Message msg = iter.message();
std::cout << endl << endl;
std::cout << level1 << "correlationID = " << msg.correlationId().asInteger() << endl;
std::cout << level1 << "messageType = " << msg.messageType().string() << endl;
std::cout << endl << endl;
Element SecurityDataArray = msg.getElement(SECURITY_DATA);
int numSecurities = SecurityDataArray.numValues();
for (int valueIndex = 0; valueIndex < numSecurities; valueIndex++)
{
Element SecurityData = SecurityDataArray.getValueAsElement(valueIndex);
string Security = SecurityData.getElementAsString(SECURITY);
std::cout << level2 << Security << endl;
bool hasFieldErrors = SecurityData.hasElement("fieldExceptions", true);
if (hasFieldErrors)
{
Element FieldErrors = SecurityData.getElement(FIELD_EXCEPTIONS);
for (size_t errorIndex = 0; errorIndex < FieldErrors.numValues(); errorIndex++)
{
Element fieldError = FieldErrors.getValueAsElement(errorIndex);
string fieldId = fieldError.getElementAsString(FIELD_ID);
Element errorInfo = fieldError.getElement(ERROR_INFO);
string source = errorInfo.getElementAsString("source");
int code = errorInfo.getElementAsInt32("code");
string category = errorInfo.getElementAsString("category");
string strMessage = errorInfo.getElementAsString("message");
string subCategory = errorInfo.getElementAsString("subcategory");
cerr << level3 << "field error:" << endl;
cerr << level4 << "fieldId = " << fieldId << endl;
cerr << level4 << "source = " << source << endl;
cerr << level4 << "code = " << code << endl;
cerr << level4 << "category = " << category << endl;
cerr << level4 << "errorMessage = " << strMessage << endl;
cerr << level4 << "subCategory = " << subCategory << endl;
}
}
bool isSecurityError = SecurityData.hasElement("securityError", true);
if (isSecurityError)
{
Element secError = SecurityData.getElement("securityError");
string source = secError.getElementAsString("source");
int code = secError.getElementAsInt32("code");
string category = secError.getElementAsString("category");
string errorMessage = secError.getElementAsString("message");
string subCategory = secError.getElementAsString("subcategory");
cerr << level3 << "security error:" << endl;
cerr << level4 << "source = " << source << endl;
cerr << level4 << "code = " << code << endl;
cerr << level4 << "category = " << category << endl;
cerr << level4 << "errorMessage = " << errorMessage << endl;
cerr << level4 << "subCategory = " << subCategory << endl;
}
else
{
Element FieldData = SecurityData.getElement(FIELD_DATA);
double pxLast = FieldData.getElementAsFloat64("PX_LAST");
double bid = FieldData.getElementAsFloat64("BID");
double ask = FieldData.getElementAsFloat64("ASK");
string ticker = FieldData.getElementAsString("TICKER");
std::cout << level3 << "fields: " << endl;
std::cout << level4 << "PX_LAST = " << pxLast << endl;
std::cout << level4 << "BID = " << bid << endl;
std::cout << level4 << "ASK = " << ask << endl;
std::cout << level4 << "TICKER = " << ticker << endl;
bool excludeNullElements = true;
if (FieldData.hasElement("CHAIN_TICKERS", excludeNullElements))
{
Element chainTickers = FieldData.getElement("CHAIN_TICKERS");
for (size_t chainTickerValueIndex = 0; chainTickerValueIndex < chainTickers.numValues(); chainTickerValueIndex++)
{
Element chainTicker = chainTickers.getValueAsElement(chainTickerValueIndex);
string strChainTicker = chainTicker.getElementAsString("Ticker");
std::cout << level4 << "CHAIN_TICKER = " << strChainTicker << endl;
}
}
else
{
std::cout << level4 << "NO CHAIN_TICKER information" << endl;
}
}
}
}
}
Regarding the second question, the Bloomberg support staff recommended me to just pick an arbitarily high number so that all options would be downloaded, i.e.
override2.setElement("fieldId", "CHAIN_POINTS_OVRD");
override2.setElement("value", 50000);
For the third question, it is possible to download the chain tickers for all maturities by setting the "CHAIN_EXP_DT_OVRD" override to 'ALL' (this part is currently untested):
Element override3 = overrides.appendElement();
override3.setElement("fieldId", "CHAIN_EXP_DT_OVRD");
override3.setElement("value", 'ALL');

C++ compiler error: ld: symbol(s) not found for member function referenced from _main

I'm very confused by a compiler error. My code was thoroughly working 4-5 h ago; the only feasible checkpoint along the way hasn't yielded any clues (i.e., I have not been able to get the error to disappear at one intermediate step). I don't see how the compiler error could be related to any of the changes I have made.
Compiling with
g++ -O3 -o a.out -I /Applications/boost_1_42_0/ Host.cpp Simulation.cpp main.cpp Rdraws.cpp SimPars.cpp
the following error appears
Undefined symbols:
"Simulation::runTestEpidSim()", referenced from:
_main in ccmcSY5M.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
I create and manipulate Simulation objects in main. My only changes to the code were (1) to create a new member function, Simulation::runTestEpidSim(), which is called in main, and (2) to write some new global input/output processing functions, which I've since unwrapped and inserted directly into main in order to debug what's going on.
I have not changed any cpp files, includes, header files, libraries, or compiler commands.
I'm not a professional programmer. How would the pros go about debugging this kind of problem?
Not sure how best to cut and paste my code, but here's an excerpt--
class Simulation
{
public:
Simulation( int trt, int sid, SimPars * spPtr );
~Simulation();
// MEMBER FUNCTION PROTOTYPES
void runDemSim( void );
void runEpidSim( void );
double runTestEpidSim( void );
...
}
int main() {
for ( int trt = 0; trt < NUM_TREATMENTS; trt++ ) {
double treatment = TREATMENTS[ trt ];
....
cout << "Treatment #" << trt + 1 << " of " << NUM_TREATMENTS << ":" << endl;
int matchAttempts = 0;
double prevError = 1.0 + PREV_ERROR_THOLD;
double thisBeta = 0.0;
while ( matchAttempts < MAX_MATCH_ATTEMPTS && prevError > PREV_ERROR_THOLD ) {
cout << " Attempt #" << matchAttempts + 1;
SimPars thesePars;
SimPars * spPtr = &thesePars;
Simulation thisSim( trt, 1, spPtr );
thisSim.runDemSim();
prevError = thisSim.runTestEpidSim() - TARGET_PREV;
cout << ", error=" << prevError << endl;
if ( prevError > PREV_ERROR_THOLD ) {
....
return 0;
}
I had previously been executing runEpidSim() with no problem.
Update
I have the full code for the implementation of Simulation::runTestEpidSim()--I have no idea how best to present this!
double Simulation::runTestEpidSim( void ) {
if ( allHosts.size() == 0 ) {
cerr << "No hosts remaining for epidemiological simulation. Cancelling." << endl;
assert(false);
}
cout << " Entering test simulation at t=" << demComplete << "." << endl;
double percentDone = 0.0;
// Initialize host population with infections
demOutputStrobe = t;
epidOutputStrobe = t;
seedInfections();
EventPQ::iterator eventIter = currentEvents.begin();
double nextTimeStep = t + EPID_DELTA_T;
double prevalences[ NUM_TEST_SAMPLES ]; // holds prevalences at strobing periods
for ( int p = 0; p < NUM_TEST_SAMPLES; p++ ) {
prevalences[ p ] = 0.0;
}
double prevYear = DEM_SIM_LENGTH + TEST_EPID_SIM_LENGTH - NUM_TEST_SAMPLES; // first simulation year to start sampling
int prevSamples = 0;
while ( t < TEST_EPID_SIM_LENGTH + demComplete )
{
#ifdef DEBUG
cout << "time step = " << t << " (" << allHosts.size() << " hosts; " << currentEvents.size() << " events queued)" << endl;
assert( currentEvents.size()>0);
#endif
// Calculate new infections for every host and add events to stack
#ifdef DEBUG
cout << "Adding infections for this time step: " << endl;
#endif
calcSI();
eventIter = currentEvents.begin();
#ifdef DEBUG
cout << "Executing events off stack (currentEvents.size()=" << currentEvents.size() << "): " << endl;
#endif
while ( ( *eventIter ).time < nextTimeStep ) {
while ( demOutputStrobe < t ) {
writeDemOutput();
demOutputStrobe += STROBE_DEM;
}
while ( epidOutputStrobe < t ) {
writeEpidOutput();
epidOutputStrobe += STROBE_EPID;
}
if ( prevYear < t ) {
prevalences[ prevSample ] = calcPrev();
cout << "\tOutputting prevalence sample #" << prevSamples+1 << "; prevalence under 5 is " << prevalences[ prevSample ] << endl;
prevSample++;
}
while ( percentDone/100.0 < ( t - demComplete )/EPID_SIM_LENGTH ) {
cout << "\t" << percentDone << "% of this test component complete." << endl;
percentDone += PROGRESS_INTERVAL;
}
// Execute events off stack
Event thisEvent = *eventIter;
#ifdef DEBUG
assert( thisEvent.time >= t );
if ( thisEvent.time < t ) {
cout << "Trying to execute event scheduled for time " << thisEvent.time << ", though sim time is " << t << endl;
assert( thisEvent.time >= t );
}
#endif
t = thisEvent.time;
#ifdef DEBUG
cout << "\tt=" << t << endl;
// cout << "\tAbout to execute event ID " << (*eventIter).eventID << " at time " << (*eventIter).time << " for host ID " << (*eventIter).hostID << endl;
cout << "\tAbout to execute event ID " << thisEvent.eventID << " at time " << thisEvent.time << " for host ID " << thisEvent.hostID << endl;
#endif
executeEvent( thisEvent );
eventCtr++;
currentEvents.erase( eventIter ); // Check that if event added to top of currentEvents, will not invalidate itr
eventIter = currentEvents.begin();
#ifdef DEBUG
cout << "\tcurrentEvents.size() after pop is " << currentEvents.size() << endl;
#endif
}
t = nextTimeStep;
nextTimeStep += EPID_DELTA_T;
}
double meanPrev = 0.0;
double sumPrev = 0.0;
int totSamples = 0;
for ( int p = 0; p < NUM_TEST_SAMPLES; p++ ) {
if ( prevalences[ p ] > 0 ) { // in cae
sumPrev += prevalences[ p ];
totSamples++;
}
}
cout << "Counted " << totSamples << " total prevalence samples." << endl;
meanPrev = sumPrev/(double)totSamples;
return( meanPrev );
}
How would the pros go about debugging
this kind of problem?
You should start with the error message:
Undefined symbols:"Simulation::runTestEpidSim()"
You've added a prototype for runTestEpidSim, but you haven't shown the definition of this function (presumably it should be in Simulation.cpp)
Here's what I'd look for, to start...
Have you defined it at all?
Have you defined it as a member of class Simulation?
Have you defined it with exactly the same parameters?
Have you defined it with exactly the same return type?
Is the definition accidentally commented or #ifdef'd out?
Update
Thanks for posting the definition. What you've posted looks fine, and passes the first four tests above - but you can see how a mismatched #ifdef DEBUG could mess things up for you...
In addition...
Have you actually compiled and linked with the file containing the definition?
The linker error message says that you don't have a definition for run*Test*EpidSim, which you need as you are calling this function inside main. Your description matches this problem: you have declared this new method, but you haven't implemented it anywhere. You need to provide the actual source code of this test function.
It looks like you declared function but not implemented it
Where did you define the runTestEpidSim? Did you define it wit the correct signature in the same file containing main? If it is defined in some other file: did you include that file in the build process?
double
Simulation::runTestEpidSim(void)
{
...
}
The code you gave contains only the declaration, so where's the definition?