Creating and using a Libray
Hi,
I am trying to create a library and distribute the library as a .so
file and a simplified header (without private data). I prepared a
basic example (see below).
The setter shows a segfault.
Maybe this is not the right way to do it? Any one knows what's wrong
here?
Thanks & Regards.
Xavi.
------------------ PRIVATE HEADER IN LIB ----------------------
#ifndef _TEST_H
#define _TEST_H
#include <vector>
using std::vector;
class Test {
private:
int testNumber;
vector<double> numbers;
public:
Test();
virtual ~Test();
const int get_testNumber() const;
void set_testNumber(int value);
void set_testNumber(vector<double> value);
};
#endif
------------------ PRIVATE CPP IN LIB ----------------------
#include "Test.h"
Test::Test() {
}
Test::~Test() {
}
const int Test::get_testNumber() const {
return testNumber;
}
void Test::set_testNumber(int value) {
testNumber = value;
}
void Test::set_testNumber(vector<double> value) {
numbers = value;
}
------------------- PUBLIC HEADER TO SEND WITH LIB -------------------
#ifndef _TEST_H
#define _TEST_H
#include <vector>
using std::vector;
class Test {
public:
Test();
virtual ~Test();
const int get_testNumber() const;
void set_testNumber(int value);
void set_testNumber(vector<double> value);
};
#endif
---------------------- Example code who uses the library
--------------------
#include "Test.h" // The public header
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(){
vector<double> v(200, 2);
Test * t = new Test();
t->set_testNumber(3);
cout<<t->get_testNumber()<<endl;
t->set_testNumber(v); // <----------- Segfault in runtime!!!!
return 0;
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]