class has no member problem (code works find in VS, but failed in Ubuntu) - class

all
I got a piece of code about sparse matrix, it works fine in VISUAL STUDIO 2008, but when I want to port it to ubuntu environment, the compiler report the following errors:
../include/spmatrix.h(123): error:
class template "numc::CSRMatrix"
has no member "resize"
../include/spmatrix.h(170): error:
expected a ";"
../include/spmatrix.h(171): error:
identifier "it" is undefined
../include/spmatrix.h(176): error:
expected a ";"
../include/spmatrix.h(178): error:
identifier "it" is undefined
../include/spmatrix.h(183): error:
identifier "size" is undefined
../include/spmatrix.h(185): error:
expected a ";"
../include/spmatrix.h(185): error:
identifier "it" is undefined
.> ./include/spmatrix.h(186): error:
expected a ";"
../include/spmatrix.h(187): error:
identifier "cit" is undefined
../include/spmatrix.h(205): error:
expected a ";"
../include/spmatrix.h(205): error:
identifier "it" is undefined
../include/spmatrix.h(218): error:
expected a ";"
../include/spmatrix.h(218): error:
identifier "it" is undefined
../include/spmatrix.h(222): error:
expected a ";"
../include/spmatrix.h(223): error:
identifier "f" is undefined
../include/spmatrix.h(224): error:
identifier "make_pair" is undefined
../include/spmatrix.h(224): error:
type name is not allowed
../include/spmatrix.h(224): error:
type name is not allowed
../include/spmatrix.h(245): error:
identifier "RowType" is undefined
../include/spmatrix.h(246): error:
name followed by "::" must be a class
or namespace name
../include/spmatrix.h(246): error:
expected a ";"
../include/spmatrix.h(246): error:
identifier "it" is undefined
../include/spmatrix.h(269): error:
identifier "RowType" is undefined
../include/spmatrix.h(270): error:
name followed by "::" must be a class
or namespace name
../include/spmatrix.h(270): error:
expected a ";"
../include/spmatrix.h(271): error:
identifier "it" is undefined
../include/spmatrix.h(276): error:
identifier "RowType" is undefined
../include/spmatrix.h(276): error:
identifier "crow" is undefined
../include/spmatrix.h(277): error:
name followed by "::" must be a class
or namespace name
../include/spmatrix.h(277): error:
expected a ";"
../include/spmatrix.h(279): error:
identifier "it" is undefined
../include/spmatrix.h(311): error:
expected a ";"
../include/spmatrix.h(311): error:
identifier "it" is undefined
../include/spmatrix.h(350): error:
expected a ";"
../include/spmatrix.h(350): error:
identifier "it" is undefined
../include/spmatrix.h(423): error:
expected a ";"
../include/spmatrix.h(424): error:
identifier "it" is undefined
../include/spmatrix.h(484): error:
expected a ";"
../include/spmatrix.h(485): error:
identifier "it" is undefined
../include/spmatrix.h(518): error:
expected a ";"
../include/spmatrix.h(520): error:
identifier "it" is undefined
I try to figure out where the problem is, but failed. Hope you can give me some hint where the problem is. Thanks in advance for your help!
Below is the code of this sparse matrix, it is just a header file:
#pragma warning(disable: 4786)
#ifndef NUMC_CSRMATRIX_H
#define NUMC_CSRMATRIX_H
#include <cmath>
#include <map>
#include <vector>
#include <limits>
#include <algorithm>
#ifndef _NUMC_BEGIN
#define _NUMC_BEGIN namespace numc {
#define _NUMC_END }
#endif
_NUMC_BEGIN
using std::vector;
using std::map;
using std::swap;
using std::lower_bound;
template<typename R> class CSRMatrix;
template<typename R> struct RowMat;
template<typename R> void CreateCSRMatrixFromRowMap(CSRMatrix<R>&, const RowMat<R>&);
t emplate<typename R> void CSRMatrixTranspose (const CSRMatrix<R>&, CSRMatrix<R>&);
template<typename R> bool Mul2MatricesSymmResult (const CSRMatrix<R>&, const CSRMatrix<R>&, RowMat<R>&);
template<typename R> bool Mul2MatricesSymmResult (const CSRMatrix<R>&, const CSRMatrix<R>&, CSRMatrix<R>&);
template<typename R> bool Mul2Matrices (const CSRMatrix<R>&, const CSRMatrix<R>&, RowMat<R>&);
template<typename R> bool Mul2Matrices (const CSRMatrix<R>&, const CSRMatrix<R>&, CSRMatrix<R>&);
template<typename R>
class CSRMatrix
{
public:
enum MatType{ RealStrucSymm=1, RealSymmPosDef=2, RealSymmIndef=-2,
CompStrucSymm=3, CompHermPosDef=4, CompHermIndef=-4,
CompSymm=6, RealUnSymm=11, CompUnSymm=13};
std::vector<R> mAv;
std::vector<int> mAi, mAj;
CSRMatrix():mNRow(0),mNCol(0),mMtype(RealUnSymm) {};
CSRMatrix(const RowMat<R> &rm):mMtype(RealUnSymm) { CreateCSRMatrixFromRowMap(*this, rm); }
MatType mMtype;
#if ( defined(_MSC_VER)&&(_MSC_VER>1300) )
//template<typename T> class CSRMatrix;
#define FFM <> // FFM FRIEND_FUNCTION_MAGIC
#else
#define FFM
#endif
friend void CreateCSRMatrixFromRowMap FFM(CSRMatrix<R>&, const RowMat<R>&);
friend void CSRMatrixTranspose FFM(const CSRMatrix<R>&, CSRMatrix<R>&);
friend bool Mul2MatricesSymmResult FFM(const CSRMatrix<R>&, const CSRMatrix<R>&, RowMat<R>&);
friend bool Mul2MatricesSymmResult FFM(const CSRMatrix<R>&, const CSRMatrix<R>&, CSRMatrix<R>&);
friend bool Mul2Matrices FFM(const CSRMatrix<R>&, const CSRMatrix<R>&, RowMat<R>&);
friend bool Mul2Matrices FFM(const CSRMatrix<R>&, const CSRMatrix<R>&, CSRMatrix<R>&);
#undef FFM
private:
int mNRow, mNCol;
public:
inline int nRow() const { return mNRow; }
inline int nCol() const { return mNCol; }
inline int empty() const { return (0==nRow() || 0==nCol()) ;}
inline bool onebase() const { return (!mAi.empty()) && (mAi[0]==1); } //default: zerobase, including empty matrix!
inline MatType mtype() const { return mMtype; }
inline bool issymm() const
{ return (RealSymmIndef==mMtype || RealSymmPosDef==mMtype || CompSymm==mMtype); }
inline void clear()
{ mNRow = 0; mNCol = 0; mMtype = RealUnSymm; mAv.clear(); mAi.clear(); mAj.clear(); }
R getElementV(int x, int y) const{
double *p = const_cast<CSRMatrix*>(this)->getElementP(x, y);
return (NULL==p)?0:*p;
}
R* getElementP(int x, int y) {
if (x<0||y<0) return NULL;
if ( issymm() && x>y) swap(x, y);
const int off = onebase();
std::vector<int>::const_iterator it = lower_bound(mAj.begin()+mAi[x]-off, mAj.begin()+(mAi[x+1]-off), y+off);
if (it==mAj.begin()+(mAi[x+1]-off) || *it!=(y+off) ) return NULL;
return &mAv.front() + ( it-mAj.begin() );
}
void MultiVect(R* in, R *out) const;
bool ChangeBase(bool oneBase) {
if( onebase() == oneBase ) return true;
int ii;
if (oneBase) {
if (mAi[0] != 0){
fprintf(stderr, "error matrix!");
return false;
}
for (ii=0; ii<mAi.back(); ii++) mAj[ii]++;
for (ii=0; ii<mNRow+1; ii++) mAi[ii]++;
}
else {
if (mAi[0] != 1){
fprintf(stderr, "error matrix!");
return false;
}
for (ii=0; ii<mAi.back()-1; ii++) mAj[ii]--;
for (ii=0; ii<mNRow+1; ii++) mAi[ii]--;
}
return true;
}
void GetSubMat(const std::vector<int> &xi, const std::vector<int> &yi, CSRMatrix& mat){
mat.resize( yi.size() );
for(int j=0; j<yi.size(); j++){
for(int i=0; i<xi.size(); i++){
}
}
}
void print(FILE *f=stdout){
fprintf(f, "The matrix:\n");
for(int i=0; i<nRow(); i++){
fprintf(f, "%2d#:", i);
for(int j=0; j<nCol(); j++) {
fprintf(f, "\t%.3e", getElementV(i, j) );
}
fprintf(f, "\n");
}
}
};
template<typename R>
struct RowMat:public vector<map<int, R> >
{
typedef map<int, R> RowType;
typedef vector<RowType> BaseType;
int mNCol;
RowMat(int nrow=0, int ncol=0):BaseType(nrow),mNCol(ncol) { }
~RowMat() { }
RowMat(const CSRMatrix<R> &mat)
{
resize(mat.nCol(), mat.nRow());
int off = mat.onebase()?1:0;
for(unsigned int i=0; i+1<mat.mAi.size(); i++){
for(int j=mat.mAi[i]; j<mat.mAi[i+1]; j++){
(*this)(i, mat.mAj[j-off]-off) = mat.mAv[ j-off ];
}
}
}
virtual bool symmetric() const {return false;}
virtual inline R operator()(int i, int j) const{
const RowType &crow = (*this)[i];
RowType::const_iterator it = crow.find(j);
return ( it==crow.end() )?0:it->second;
}
virtual inline R& operator()(int i, int j){
RowType &crow = (*this)[i];
RowType::iterator it = crow.find(j);
return ( it==crow.end() )?crow.insert( std::pair<int,R>(j, 0) ).first->second:it->second;
}
int clearZero() {
int nZero;
for (size_t i=0; i<size(); i++) {
RowType &r = (*this)[i];
for (RowType::const_iterator it=r.begin(); it!=r.end(); ) {
RowType::const_iterator cit = it++;
if(fabs(cit->second) < std::numeric_limits<R>::epsilon()){
r.erase(cit);
++nZero;
}
}
}
return nZero;
}
inline void resize(int nrow, int ncol=0) { BaseType::resize(nrow); mNCol = ncol>0?ncol:mNCol; }
inline int nRow() const { return BaseType::size(); }
inline int nCol() const { return mNCol; }
int operator+=(const RowMat<R>& r) { return add(r, 1); }
void operator *= (const double v) {
for(int i=0; i<nRow(); i++){
map<int, R>& crow = (*this)[i];
for(map<int,R>::iterator it=crow.begin(); it!=crow.end(); ++it){
it->second *= v;
}
}
}
int add(const RowMat<R>& r, R k){
const int h = nRow();
if( r.nRow() != h ) return -1;
for(int i=0; i<h; i++){
const map<int, R>& rrow = r[i];
map<int, R>& lrow = (*this)[i];
for(RowType::const_iterator it=rrow.begin(); it!=rrow.end(); ++it){
int col = it->first;
R val = it->second * k;
map<int,R>::iterator f = lrow.find(col);
if( f != lrow.end() ) f->second += val;
lrow.insert( make_pair<int, R>(col, val) );
}
}
return 0;
}
};
template<typename R>
struct RowMatSym : public RowMat<R>
{
typedef RowMat<R> Base;
RowMatSym(int nrow=0, int ncol=0):Base(nrow, ncol) {}
~RowMatSym() { }
RowMatSym(const RowMat<R> &rm){
resize(rm.nCol(), rm.nRow());
for(unsigned int i=0; i<rm.nRow(); i++){
const RowType &crow = rm[i];
for(RowType::const_iterator it=crow.begin(); it!=crow.end(); ++it){
(*this)(i, it->first) = it->second;
}
}
}
RowMatSym(const CSRMatrix<R> &mat)
{
resize(mat.nCol(), mat.nRow());
int off = mat.onebase()?1:0;
for(unsigned int i=0; i+1<mat.mAi.size(); i++){
for(int j=mat.mAi[i]; j<mat.mAi[i+1]; j++){
const int k = mat.mAj[j-off]-off;
(*this)(i, k) = mat.mAv[ j-off ];
}
}
}
virtual bool symmetric() const {return true;}
inline R operator()(int i, int j) const{
if(i>j) std::swap(i, j);
const RowType &crow = (*this)[i];
RowType::const_iterator it = crow.find(j);
return ( it==crow.end() )?0:it->second;
}
inline R& operator()(int i, int j){
if(i>j) std::swap(i, j);
RowType &crow = (*this)[i];
RowType::iterator it = crow.find(j);
return ( it==crow.end() )?crow.insert( std::pair<int,R>(j, 0) ).first->second
:it->second;
}
};
//////////////////////////////////////////////////////////////////////////
/// Get SubMatrix from rowmap
//////////////////////////////////////////////////////////////////////////
template <typename R>
void GetSubRowMap(const RowMat<R>& src,
const std::vector<int> &xi, const std::vector<int> &yi,
RowMat<R>& dst)
{
assert( xi.size()>0 && yi.size()>0 );
assert( yi.back() < (int)src.size() );
if( xi.empty() || yi.empty() || yi.back() >= (int)src.size() ){
fprintf(stderr, "\nerror matrix!");
return;
}
typedef std::map<int, R> row;
dst.resize( yi.size() );
for(unsigned int j=0; j<yi.size(); j++){
const row &srow = src[ yi[j] ];
row &drow = dst[j];
const int* pxi = &xi.front();
for(row::const_iterator it=srow.begin(); it!=srow.end(); ++it ){
pxi = std::lower_bound(pxi, &xi.back(), it->first);
if(*pxi != it->first){ // done
continue;
}
int col = pxi - &xi.front();
drow[col] = it->second;
}
}
}
//////////////////////////////////////////////////////////////////////////
/// Create CSR matrix from vector of rowmap
//////////////////////////////////////////////////////////////////////////
template <typename R>
void CreateCSRMatrixFromRowMap(CSRMatrix<R> &matC, const RowMat<R>& rowC)
{
if(rowC.mNCol <= 0){
fprintf(stderr, "\nnumber of column not specified in the RowMat, exit!");
return;
}
matC.clear();
matC.mNRow = rowC.nRow();
matC.mNCol = rowC.nCol();
matC.mAi.reserve(matC.nRow()+1);
int i,nnz=0;
for (i=0; i<matC.nRow(); i++) {
nnz += rowC[i].size();
}
matC.mAj.reserve(nnz);
matC.mAv.reserve(nnz);
// copy rows into matC
matC.mAi.push_back(0);
for (i=0; i<matC.nRow(); i++) {
matC.mAi.push_back(matC.mAi.back());
for (std::map<int, R>::const_iterator it=rowC[i].begin(); it!=rowC[i].end(); it++) {
matC.mAi.back()++;
matC.mAj.push_back(it->first);
matC.mAv.push_back(it->second);
}
}
}
//////////////////////////////////////////////////////////////////////////
/// Computes the transpose of a matrix.
template <typename R>
void CSRMatrixTranspose(const CSRMatrix<R> &matA, CSRMatrix<R> &matAT)
{
if (matA.issymm()) { // symmetric - just copy the matrix
matAT = matA;
return;
}
matAT.mNRow = matA.nCol();
matAT.mNCol = matA.nRow();
matAT.mMtype = matA.mMtype;
// non-symmetric matrix -> need to build data structure.
// we'll go over the columns and build the rows
int off = matA.onebase()?1:0;
RowMat<R> rowC(matA.nCol(), matA.nRow());
for (int i=0; i<matA.nRow(); i++) {
for (int j=matA.mAi[i]; j<matA.mAi[i+1]; j++) {
rowC[matA.mAj[j-off]-off][i] = matA.mAv[j-off];
}
}
CreateCSRMatrixFromRowMap(matAT, rowC);
}
//////////////////////////////////////////////////////////////////////////
// multiplication of sparse matrix
// Assuming nothing about the result (the result is not stored symmetric)
//////////////////////////////////////////////////////////////////////////
template <typename R>
bool Mul2Matrices(const CSRMatrix<R> &matA, const CSRMatrix<R> &matB,
RowMat<R> &rowsC)
{
if(matA.onebase() || matB.onebase() ){
fprintf(stderr, "\nmatrix saved in 1-based format, pleased converted it to 0-based and try again!");
return false;
}
// Compatibility of dimensions
if (matA.nCol() != matB.nRow())
return false;
// (m x n)*(n x k) = (m x k)
const int m=matA.nRow();
const int n=matA.nCol();
const int k=matB.nCol();
rowsC = RowMat<R>(m, k); // clean all coefficients
R aiv, valB;
int colInd, colB;
for (int i=0; i<m; ++i) { // creating row i of C
std::map<int, R> &mapRow2Val = rowsC[i];
for (int iAi = matA.mAi[i]; iAi < matA.mAi[i+1]; ++iAi) { // travel on ai
colInd = matA.mAj[iAi];
aiv = matA.mAv[iAi];
// make aiv*b_{rowInd} and insert into mapRow2Val
for (int iB=matB.mAi[colInd]; iB<matB.mAi[colInd+1]; ++iB) {
colB=matB.mAj[iB];
valB=matB.mAv[iB];
// insert valA*aiv into map
std::map<int, R>::iterator it = mapRow2Val.find(colB);
if (it == mapRow2Val.end()) { // first time
mapRow2Val[colB] = valB*aiv;
}
else {
it->second = it->second + valB*aiv;
}
}
}
}// now column i is created
return true;
}
template <typename R>
bool Mul2Matrices(const CSRMatrix<R> &matA, const CSRMatrix<R> &matB, CSRMatrix<R> &matC)
{
RowMat<R> rowsC;
if( !Mul2Matrices(matA, matB, rowsC) ) return false;
const int k=matB.mNCol;
rowsC.mNCol = k;
CreateCSRMatrixFromRowMap(matC, rowsC); // modified by jianwei hu # 16/09/07
return true;
}
//////////////////////////////////////////////////////////////////////////
// multiplication of sparse matrix
// The result is symmetric
//////////////////////////////////////////////////////////////////////////
template <typename R>
bool Mul2MatricesSymmResult(const CSRMatrix<R> &matA, const CSRMatrix<R> &matB,
RowMat<R> &rowsC)
{
if(matA.onebase() || matB.onebase()){
fprintf(stderr, "\nmatrix saved in 1-based format, pleased converted it to 0-based and try again!");
return false;
}
// Compatibility of dimensions
if(matA.nCol() != matB.nRow() || matA.nRow() != matB.nCol()) return false;
// (m x n)*(n x m) = (m x m)
const int m=matA.nRow();
const int n=matA.nCol();
rowsC = RowMat<R>(m, m); // clean all coefficients
R aiv, valB;
int colInd, colB;
for(int i=0; i<m; ++i) { // creating row i of C
std::map<int, R> &mapRow2Val = rowsC[i];
for (int iAi = matA.mAi[i]; iAi < matA.mAi[i+1]; ++iAi) { // travel on ai
colInd = matA.mAj[iAi];
aiv = matA.mAv[iAi];
// make aiv*b_{colInd} and insert into mapRow2Val
for (int iB=matB.mAi[colInd]; iB<matB.mAi[colInd+1]; ++iB) {
colB=matB.mAj[iB];
if (colB >= i) {
valB=matB.mAv[iB];
// insert valA*aiv into map
std::map<int, R>::iterator it = mapRow2Val.find(colB);
if (it == mapRow2Val.end()) { // first time
mapRow2Val[colB] = valB*aiv;
}
else {
it->second = it->second + valB*aiv;
}
}
}
}
}// now column i is created
return true;
}
template <typename R>
bool Mul2MatricesSymmResult(const CSRMatrix<R> &matA, const CSRMatrix<R> &matB, CSRMatrix<R> &matC)
{
RowMat<R> rowsC;
if( !Mul2MatricesSymmResult(matA, matB, rowsC) ) return false;
rowsC.mNCol = matA.nCol();
CreateCSRMatrixFromRowMap(matC, rowsC);
matC.mMtype = CSRMatrix<R>::RealSymmIndef;
return true;
}
template<typename R>
void DebugShowRowMatrix(const std::vector<std::map<int,R> > &rowA)
{
printf("\n");
for(unsigned int i=0; i<rowA.size(); i++){
printf("\n%3d#", i);
std::map<int,R>::const_iterator it=rowA[i].begin();
int j=0;
for(; it!=rowA[i].end(); ++it){
for(int k=j; k<it->first; k++){
printf("\t%3.2f", 0);
}
j=it->first+1;
printf("\t%3.2f", it->second);
}
}
}
typedef CSRMatrix<float> CSRMatrixf;
typedef CSRMatrix<double> CSRMatrixd;
_NUMC_END
#endif // NUMC_CSRMATRIX_H

Line 123: the CSRMatrix::resize method is undefined, and since CSRMatrix has no base class, it's not inherited.
Line 171: Using a typedef's nested type, like RowType::const_iterator, is a Visual Studio extension, it may not work in other compilers. You should replace these and similar usages with other typedefs, like so:
typedef typename map<int,R>::const_iterator RowTypeConstIterator;
UPDATE: The typename keyword is required (just learned it!)

Related

Sort an array A using Quick Sort. Using reccursion

#include<iostream>
using namespace std;
void quickSort(int input[], int start, int end)
{
// your code goes here
}
void quickSort(int input[], int size)
{
quickSort(input, 0, size - 1);
}
*/
void swap(int* a,int* b){
int temp=*a;
*a=*b;
*b=temp;
}
int count(int input[],int start,int end ){
static int c=0;
if(start==end)
return c;
if(input[start]>input[end])
c++;
return count(input,start,end-1);
}
int partionArray(int input[],int start,int end ){
int c=count(input,start,end);
int pi=c+start;
swap(&input[start],&input[pi]);
int i=start;
int j=end;
while(i<pi&&j>pi)
{
if(input[i]<input[pi])
{
i++;
}
else if(input[j]>=input[pi])
{
j--;
}
else
{
swap(&input[i],&input[j]);
i++;
j--;
}
}
return pi;
}
void qs(int input[],int start, int end){
if(start>=end)
return;
int pi=partionArray(input,start,end);
qs(input,start,pi-1);
qs(input,pi+1,end);
}
void quickSort(int input[], int size) {
qs(input,0,size-1);
}
int main(){
int n;
cin >> n;
int *input = new int[n];
for(int i = 0; i < n; i++) {
cin >> input[i];
}
quickSort(input, n);
for(int i = 0; i < n; i++) {
cout << input[i] << " ";
}
delete [] input;
}
Sort an array A using Quick Sort. Using reccursion is the question.
Input format :
Line 1 : Integer n i.e. Array size
Line 2 : Array elements (separated by space)
Output format :
Array elements in increasing order (separated by space)
Constraints :
1 <= n <= 10^3
What did i do wrong in this code pls can any one explain?Is every thing right with this code?

Doxygen doesn't generate References function call relation for class method whose object created indirectly

References to Array<>::print is missing for testTemplate in document generated for below example documentation:
.
#include"lib.h"
using namespace std;
void testTemplate(void);
int main()
{
testTemplate();
return 0;
}
void testTemplate(void)
{
int arr[5] = {1, 2, 3, 4, 5};
using array_type = Array<int>;
array_type array2(arr, 5);
array2.print();
}
#include<iostream>
template <typename T>
class Array
{
private:
T *ptr;
int size;
public:
Array(T arr[], int s);
void print();
};
template <typename T>
Array<T>::Array(T arr[], int s)
{
ptr = new T[s];
size = s;
for(int i = 0; i < size; i++)
{
ptr[i] = arr[i];
}
}
template <typename T>
void Array<T>::print()
{
for (int i = 0; i < size; i++)
{
std::cout<<" "<<*(ptr + i);
}
std::cout<<std::endl;
}
Doxygen settings:
EXTRACT_ALL = YES
EXTRACT_PRIVATE = YES
EXTRACT_PACKAGE = YES
EXTRACT_STATIC = YES
EXTRACT_LOCAL_CLASSES = YES
EXTRACT_LOCAL_METHODS = YES
EXTRACT_ANON_NSPACES = YES
RESOLVE_UNNAMED_PARAMS = YES
REFERENCED_BY_RELATION = YES
REFERENCES_RELATION = YES
Exmaple 2: similar use case
file <project_name.h>
class ProjectName
{
public:
int arr[5] = {1, 2, 3, 4, 5};
Array<int> array_type;
ProjectName(void):array_type(arr,5)
{
}
void print(void)
{
std::cout<<"Hello World"<<std::endl;
}
};
ProjectName& getProjectName(void);
file <project_name.cpp>
#include "project_name.h"
ProjectName& getProjectName(void)
{
static ProjectName obj;
return obj;
}
file <main.cpp>
#include <iostream>
#include"project_name.h"
using namespace std;
void testTemplate(void);
int main()
{
testTemplate();
return 0;
}
void testTemplate(void)
{
ProjectName& project = getProjectName();
project.print();
project.array_type.print();
}
References to Array<>::print is missing for testTemplate in generated document :

QTreeView: Drag-And-Drop fails to call QAbstractItemModel::dropMineData (source code in the post)

I am building an app with a treeview and I created a model which derives from QAbstractItemModel. I can manipulate the tree by dropping a file (dragging the file from outside of the app into the app) and drag-and-drop items within the tree.
The problem is that the file drop into the app doesn't always work since I re-implemented the function mimeData(const QModelIndexList &indexes). Sometimes and only when there is a selected item in the tree (which provides a valid item index to the function mimeData), the file drop doesn't work because the function mimeData is called but the function dropMimeData is never called.
I had to re-implement the function mimeData to be able to create my own mimeData which is used during drag-and-drop within the tree. This mimeData is used in the function dropMimeData, which works fine.
It looks like that the function mimeData should not be called during the file drop as the application knows already the mimeData format: text/uri-list.
My function dropMimeData processes both my own mimeData format and text/uri-list format.
Does anybody have the same issue or have any thoughts on this?
Again it is not like if it was not working at all, it only fails some of the time.
Any help or thoughts would be great.
Cheers.
The following might be totally unrelated, it is something I came across while trying to debug this. It looks like that there might be relation with the state of the window: QEvent::WindowActivate or QEvent::WindowDeactivate. During the file drop, the window becomes inactive and active again but the problem seems to occur when the window fails to become active again. ?
I added the source code to reproduce the problem. Drop a file into the app. Select the item in the tree. Drop more files. Sometime the drop doesn't work???
// main.cpp
#include "dragdropmainwindow.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DragDropMainWindow w;
w.show();
return a.exec();
}
// dragdropmainwindow.h
#ifndef DRAGDROPMAINWINDOW_H
#define DRAGDROPMAINWINDOW_H
#include <QtGui/QMainWindow>
#include "ui_dragdropmainwindow.h"
#include "ControlTreeView.h"
class DragDropMainWindow : public QMainWindow
{
Q_OBJECT
public:
DragDropMainWindow(QWidget *parent = 0, Qt::WFlags flags = 0);
~DragDropMainWindow();
private:
Ui::DragDropMainWindowClass m_ui;
ControlTreeView *m_controlTree;
};
#endif // DRAGDROPMAINWINDOW_H
// dragdropmainwindow.cpp
#include "dragdropmainwindow.h"
DragDropMainWindow::DragDropMainWindow(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
m_ui.setupUi(this);
// Create a model to control the view
m_controlTree = new ControlTreeView(NULL);
m_ui.m_treeView->setModel(m_controlTree);
}
DragDropMainWindow::~DragDropMainWindow()
{
}
// ControlTreeView.h
#pragma once
#include <QAbstractItemModel>
#include <QModelIndex>
#include <QStringList>
#include <QVector>
#include <QList>
class TreeItem
{
public:
TreeItem(const QVector<QVariant> &data, TreeItem *parent = 0);
~TreeItem();
TreeItem *child(int number);
int childCount() const;
int columnCount() const;
QVariant data(int column) const;
bool insertChildren(int position, int count, int columns);
bool insertColumns(int position, int columns);
TreeItem *parent();
bool removeChildren(int position, int count);
bool removeColumns(int position, int columns);
int childNumber() const;
bool setData(int column, const QVariant &value);
private:
QList<TreeItem*> childItems;
QVector<QVariant> itemData;
TreeItem *parentItem;
};
class ControlTreeView : public QAbstractItemModel
{
Q_OBJECT
public:
enum TREE_COLUMNS
{
eCOLUMN_FILENAME
};
ControlTreeView(QObject *parent);
~ControlTreeView(void);
QModelIndex index (int row, int column, const QModelIndex & parent = QModelIndex()) const;
QModelIndex parent (const QModelIndex & index) const;
int rowCount (const QModelIndex & parent = QModelIndex()) const;
int columnCount (const QModelIndex & parent = QModelIndex()) const;
QVariant data (const QModelIndex & index, int role = Qt::DisplayRole) const;
Qt::ItemFlags flags (const QModelIndex & index) const;
bool setData (const QModelIndex & index, const QVariant & value, int role = Qt::EditRole);
QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
bool insertRows (int row, int count, const QModelIndex & parent = QModelIndex());
//bool insertColumns (int column, int count, const QModelIndex & parent = QModelIndex());
bool removeRows (int row, int count, const QModelIndex & parent = QModelIndex());
//bool removeColumns (int column, int count, const QModelIndex & parent = QModelIndex());
bool dropMimeData (const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent);
Qt::DropActions supportedDropActions() const;
QStringList mimeTypes () const;
QMimeData *mimeData(const QModelIndexList &indexes) const;
bool setHeaderData(int section, Qt::Orientation orientation,
const QVariant &value, int role);
private:
TreeItem *getItem(const QModelIndex &jobIndex) const;
TreeItem *m_rootItem; // Root of the view tree
};
// ControlTreeView.cpp
#include "ControlTreeView.h"
#include <QtGui>
#include <QtGui/QGraphicsView>
class TreeColumn
{
public:
ControlTreeView::TREE_COLUMNS m_enumColumn;
QString m_header;
int m_index;
};
static const int NUMBER_COLUMNS = 1;
static TreeColumn s_columns[NUMBER_COLUMNS];
TreeItem::TreeItem(const QVector<QVariant> &data, TreeItem *parent)
{
parentItem = parent;
itemData = data;
}
TreeItem::~TreeItem()
{
qDeleteAll(childItems);
}
TreeItem *TreeItem::parent()
{
return parentItem;
}
TreeItem *TreeItem::child(int number)
{
return childItems.value(number);
}
int TreeItem::childCount() const
{
return childItems.count();
}
int TreeItem::childNumber() const
{
if (parentItem)
return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
return 0;
}
int TreeItem::columnCount() const
{
return itemData.count();
}
QVariant TreeItem::data(int column) const
{
return itemData.value(column);
}
bool TreeItem::setData(int column, const QVariant &value)
{
if (column < 0 || column >= itemData.size())
return false;
itemData[column] = value;
return true;
}
bool TreeItem::insertChildren(int position, int count, int columns)
{
if (position < 0 || position > childItems.size())
return false;
for (int row = 0; row < count; ++row)
{
QVector<QVariant> data(columns);
TreeItem *item = new TreeItem(data, this);
childItems.insert(position, item);
}
return true;
}
bool TreeItem::removeChildren(int position, int count)
{
if (position < 0 || position + count > childItems.size())
return false;
for (int row = 0; row < count; ++row)
delete childItems.takeAt(position);
return true;
}
bool TreeItem::insertColumns(int position, int columns)
{
if (position < 0 || position > itemData.size())
return false;
for (int column = 0; column < columns; ++column)
itemData.insert(position, QVariant());
foreach (TreeItem *child, childItems)
child->insertColumns(position, columns);
return true;
}
ControlTreeView::ControlTreeView(QObject *parent) :
QAbstractItemModel(parent)
{
QStringList headers;
s_columns[ControlTreeView::eCOLUMN_FILENAME].m_header = QObject::tr("File");
s_columns[ControlTreeView::eCOLUMN_FILENAME].m_index = 0;
for (unsigned int index = 0; index < NUMBER_COLUMNS; index++)
headers.append(s_columns[index].m_header);
QVector<QVariant> rootData;
foreach (QString header, headers)
rootData << header;
m_rootItem = new TreeItem(rootData);
}
ControlTreeView::~ControlTreeView()
{
delete m_rootItem;
}
int ControlTreeView::columnCount(const QModelIndex & parent) const
{
return NUMBER_COLUMNS;
};
TreeItem *ControlTreeView::getItem(const QModelIndex &index) const
{
if (index.isValid())
{
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if (item) return item;
}
return m_rootItem;
}
int ControlTreeView::rowCount(const QModelIndex &parent) const
{
TreeItem *parentItem = getItem(parent);
return parentItem->childCount();
}
Qt::ItemFlags ControlTreeView::flags(const QModelIndex &index) const
{
//if (!index.isValid())
// return 0;
//return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled;
Qt::ItemFlags defaultFlags = Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
if (index.isValid())
return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
else
return Qt::ItemIsDropEnabled | defaultFlags;
}
QModelIndex ControlTreeView::index(int row, int column, const QModelIndex &parent) const
{
if (parent.isValid() && parent.column() != 0)
return QModelIndex();
TreeItem *parentItem = getItem(parent);
TreeItem *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
QModelIndex ControlTreeView::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
TreeItem *childItem = getItem(index);
TreeItem *parentItem = childItem->parent();
if (parentItem == m_rootItem)
return QModelIndex();
return createIndex(parentItem->childNumber(), 0, parentItem);
}
QVariant ControlTreeView::data(const QModelIndex & index, int role) const
{
if (!index.isValid())
return QVariant();
switch (role)
{
case Qt::DisplayRole:
case Qt::EditRole:
{
TreeItem *item = getItem(index);
return item->data(index.column());
}
break;
case Qt::DecorationRole:
{
if (index.column() == 0)
{
if (!index.parent().isValid()) // Only decorate for
{
//QString file = m_jobList->GetJobSrcFileName(index.row());
//if (!file.isEmpty())
// return IconCache::Get().Load(file);
}
}
}
break;
}
return QVariant();
}
bool ControlTreeView::setHeaderData(int section, Qt::Orientation orientation,
const QVariant &value, int role)
{
if (role != Qt::EditRole || orientation != Qt::Horizontal)
return false;
bool result = m_rootItem->setData(section, value);
if (result)
emit headerDataChanged(orientation, section, section);
return result;
}
QVariant ControlTreeView::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return m_rootItem->data(section);
return QVariant();
}
QStringList ControlTreeView::mimeTypes () const
{
QStringList mimeTypes;
mimeTypes += "text/uri-list";
mimeTypes += "text/conversion-job-tree";
return mimeTypes;
}
QMimeData *ControlTreeView::mimeData(const QModelIndexList &indexes) const
{
qDebug() << __FUNCTION__;
QMimeData *mimeData = 0;
// If the window is out off focus a file is dragged from outside of the app
// This is not normal behaviour as this function should not be call if
// the drag comes from outside of the app (??).
mimeData = new QMimeData();
QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
foreach (QModelIndex index, indexes) {
if (index.isValid() && !index.parent().isValid())
{
stream << index.row();
}
else
if (index.parent().isValid())
stream << -1;
}
mimeData->setData("text/conversion-job-tree", encodedData);
return mimeData;
}
Qt::DropActions ControlTreeView::supportedDropActions() const
{
return Qt::CopyAction;
}
bool ControlTreeView::dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent)
{
qDebug() << __FUNCTION__;
if (action == Qt::IgnoreAction)
return true;
bool success = false;
if (action == Qt::CopyAction)
{
if (data->hasFormat("text/uri-list"))
{
QList<QUrl> urls = data->urls();
for (int i = 0; i < urls.size(); ++i)
{
QString file = urls[i].toLocalFile();
if (file != "")
{
// Check if the file is already in the table
int insertRow = -1;
if (parent.isValid())
insertRow = parent.row();
if (insertRow == -1)
if (row == -1)
insertRow = m_rootItem->childCount();
else
insertRow = row;
insertRows(insertRow, 1, QModelIndex());
setData(index(insertRow, s_columns[ControlTreeView::eCOLUMN_FILENAME].m_index), file, Qt::EditRole);
success = true;
}
}
}
else
{
// Different MineData used for drop tree items
if (data->hasFormat("text/conversion-job-tree"))
{
// Only drop on a job
if (parent.isValid() && parent.parent().isValid())
return false;
int insertRow = -1;
if (parent.isValid())
insertRow = parent.row();
if (insertRow == -1)
if (row == -1)
insertRow = m_rootItem->childCount();
else
insertRow = row;
QByteArray encodedData = data->data("text/conversion-job-tree");
QDataStream stream(&encodedData, QIODevice::ReadOnly);
int srcRowIndex = -1;
while (!stream.atEnd()) {
stream >> srcRowIndex;
}
// Exit if the source is not valid
if (srcRowIndex == -1)
return false;
if (srcRowIndex < insertRow)
{
insertRow--;
if (insertRow < 0)
insertRow = 0;
}
if (srcRowIndex == insertRow)
return false;
// Remove the original raw
removeRows(srcRowIndex,1);
// Insert a new raw
insertRows(insertRow, 1, QModelIndex());
//setData(index(insertRow, s_columns[ConversionJobTreeViewCtrl::eCOLUMN_FILENAME].m_index), m_jobList->At(insertRow)->GetBaseSrcFileName(), Qt::EditRole);
return success;
}
}
}
else
success = false;
return success;
}
bool ControlTreeView::insertRows(int position, int rows, const QModelIndex &parent)
{
TreeItem *parentItem = getItem(parent);
bool success;
beginInsertRows(parent, position, position + rows - 1);
success = parentItem->insertChildren(position, rows, m_rootItem->columnCount());
endInsertRows();
return success;
}
bool ControlTreeView::removeRows(int position, int rows, const QModelIndex &parent)
{
TreeItem *parentItem = getItem(parent);
bool success = true;
beginRemoveRows(parent, position, position + rows - 1);
success = parentItem->removeChildren(position, rows);
endRemoveRows();
return success;
}
bool ControlTreeView::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role != Qt::EditRole)
return false;
TreeItem *item = getItem(index);
bool result = item->setData(index.column(), value);
if (result)
emit dataChanged(index, index);
return result;
}

Are there simple examples of how to parse a simple JSON file using the C-based YAJL library?

I know there is an Objective-C wrapper around YAJL, but this is a really fat thing which blows up the whole JASON parser to a ridiculous huge amount of 21 files, many of them with tiny scroll bars.
So to keep my app binary small I'd like to stick with the C-version of that parser. But I'm having a hard time finding any useful documentation for this rather than the wrapper.
Maybe someone who used the C-base can point out such a tutorial or documentation?
The documentation with C examples can be found here: http://lloyd.github.com/yajl/
The github repository with examples can be found here : https://github.com/lloyd/yajl
Here is a C example that reformats JSON from stdin:
#include <yajl/yajl_parse.h>
#include <yajl/yajl_gen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int reformat_null(void * ctx)
{
yajl_gen g = (yajl_gen) ctx;
return yajl_gen_status_ok == yajl_gen_null(g);
}
static int reformat_boolean(void * ctx, int boolean)
{
yajl_gen g = (yajl_gen) ctx;
return yajl_gen_status_ok == yajl_gen_bool(g, boolean);
}
static int reformat_number(void * ctx, const char * s, size_t l)
{
yajl_gen g = (yajl_gen) ctx;
return yajl_gen_status_ok == yajl_gen_number(g, s, l);
}
static int reformat_string(void * ctx, const unsigned char * stringVal,
size_t stringLen)
{
yajl_gen g = (yajl_gen) ctx;
return yajl_gen_status_ok == yajl_gen_string(g, stringVal, stringLen);
}
static int reformat_map_key(void * ctx, const unsigned char * stringVal,
size_t stringLen)
{
yajl_gen g = (yajl_gen) ctx;
return yajl_gen_status_ok == yajl_gen_string(g, stringVal, stringLen);
}
static int reformat_start_map(void * ctx)
{
yajl_gen g = (yajl_gen) ctx;
return yajl_gen_status_ok == yajl_gen_map_open(g);
}
static int reformat_end_map(void * ctx)
{
yajl_gen g = (yajl_gen) ctx;
return yajl_gen_status_ok == yajl_gen_map_close(g);
}
static int reformat_start_array(void * ctx)
{
yajl_gen g = (yajl_gen) ctx;
return yajl_gen_status_ok == yajl_gen_array_open(g);
}
static int reformat_end_array(void * ctx)
{
yajl_gen g = (yajl_gen) ctx;
return yajl_gen_status_ok == yajl_gen_array_close(g);
}
static yajl_callbacks callbacks = {
reformat_null,
reformat_boolean,
NULL,
NULL,
reformat_number,
reformat_string,
reformat_start_map,
reformat_map_key,
reformat_end_map,
reformat_start_array,
reformat_end_array
};
static void
usage(const char * progname)
{
fprintf(stderr, "%s: reformat json from stdin\n"
"usage: json_reformat [options]\n"
" -m minimize json rather than beautify (default)\n"
" -u allow invalid UTF8 inside strings during parsing\n",
progname);
exit(1);
}
int
main(int argc, char ** argv)
{
yajl_handle hand;
static unsigned char fileData[65536];
/* generator config */
yajl_gen g;
yajl_status stat;
size_t rd;
int retval = 0;
int a = 1;
g = yajl_gen_alloc(NULL);
yajl_gen_config(g, yajl_gen_beautify, 1);
yajl_gen_config(g, yajl_gen_validate_utf8, 1);
/* ok. open file. let's read and parse */
hand = yajl_alloc(&callbacks, NULL, (void *) g);
/* and let's allow comments by default */
yajl_config(hand, yajl_allow_comments, 1);
/* check arguments.*/
while ((a < argc) && (argv[a][0] == '-') && (strlen(argv[a]) > 1)) {
unsigned int i;
for ( i=1; i < strlen(argv[a]); i++) {
switch (argv[a][i]) {
case 'm':
yajl_gen_config(g, yajl_gen_beautify, 0);
break;
case 'u':
yajl_config(hand, yajl_dont_validate_strings, 1);
break;
default:
fprintf(stderr, "unrecognized option: '%c'\n\n",
argv[a][i]);
usage(argv[0]);
}
}
++a;
}
if (a < argc) {
usage(argv[0]);
}
for (;;) {
rd = fread((void *) fileData, 1, sizeof(fileData) - 1, stdin);
if (rd == 0) {
if (!feof(stdin)) {
fprintf(stderr, "error on file read.\n");
retval = 1;
}
break;
}
fileData[rd] = 0;
stat = yajl_parse(hand, fileData, rd);
if (stat != yajl_status_ok) break;
{
const unsigned char * buf;
size_t len;
yajl_gen_get_buf(g, &buf, &len);
fwrite(buf, 1, len, stdout);
yajl_gen_clear(g);
}
}
stat = yajl_complete_parse(hand);
if (stat != yajl_status_ok) {
unsigned char * str = yajl_get_error(hand, 1, fileData, rd);
fprintf(stderr, "%s", (const char *) str);
yajl_free_error(hand, str);
retval = 1;
}
yajl_gen_free(g);
yajl_free(hand);
return retval;
}

ld: Symbol(s) not found… but they are there…

My project is really acting up lately - I ironed out a whole bunch of errors, and then it throws a cryptic ld: symbol(s) not found error. However, it's clear that the symbols do exist:
Undefined symbols:
"Solid::~Solid()", referenced from:
void std::_Destroy<Solid>(Solid*)in ui.o
"Log::operator+=(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
runUI() in ui.o
"Log::getLog(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
runUI() in ui.o
"Log::Log()", referenced from:
__static_initialization_and_destruction_0(int, int)in ui.o
"Building::Building(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
runUI() in ui.o
"Log::~Log()", referenced from:
___tcf_1 in ui.o
Here are the files it references:
log.cpp
/*
* log.cpp
*
* Created on: Apr 30, 2011
* Author: wjc
*/
#include <string>
#include <vector>
#include <sstream> // for converting count to string
#include "consts.h"
using namespace std;
struct logResult {
vector <string> result;
int status;
};
struct parsedLog {
string result;
int status;
};
class Log {
private:
bool initialized;
vector <string> * actionLog;
static int count;
string countString(){
stringstream ss;
ss<<count;
return ss.str();
}
public:
int initialize(){
if (initialized) return ALREADY_INIT;
actionLog->push_back("*** Log initialized ***");
actionLog->push_back("This is log#" + countString() +". If this is greater than one, there is a problem.");
initialized = true;
return SUCCESS;
}
int initialize(string text){
int initResult = initialize();
if (initResult == ALREADY_INIT) return ALREADY_INIT;
actionLog->push_back("Initialization message: "+text);
return SUCCESS;
}
Log (){
initialize();
count++;
}
Log (string text){
initialize(text);
count++;
}
~Log (){
count--;
}
bool isInitialized(){
return initialized;
}
int add(string text){
if (!initialized) return NOT_INIT;
actionLog->push_back(text);
return SUCCESS;
}
int operator+= (string text){
return add(text);
}
int clearLog(bool init = true){
if (!initialized) return NOT_INIT;
delete actionLog;
int initResult = SUCCESS;
if (init) initResult = initialize();
if (initResult == ALREADY_INIT) return ALREADY_INIT;
// Otherwise
// (no other possibilities because initialize()
// only returns either a SUCCESS or
// ALREADY_INIT value)
return SUCCESS;
}
logResult getLog(){
if (!initialized){
logResult final;
final.status = NOT_INIT;
return final;
} else {
logResult final;
final.result = *actionLog;
final.status = SUCCESS;
return final;
}
}
parsedLog getLog(string delim){
if (!initialized){
parsedLog final;
final.status = NOT_INIT;
return final;
} else {
parsedLog final;
string logString;
for (unsigned int i; i<actionLog->size()-1; i++){
logString += (*actionLog)[i];
logString += delim;
}
logString += actionLog->back();
final.result = logString;
final.status = SUCCESS;
return final;
}
}
};
log.h
/*
* log.h
*
* Created on: Apr 30, 2011
* Author: wjc
*/
#ifndef LOG_H_
#define LOG_H_
#include <string>
using namespace std;
struct logResult {
vector <string> result;
int status;
};
struct parsedLog {
string result;
int status;
};
class Log {
public:
int initialize();
int initialize(string text);
Log ();
Log (string text);
~Log();
bool isInitialized;
int add(string text);
int operator+= (string text);
int clearLog (bool init = true);
vector <string> getLog();
parsedLog getLog(string delim);
private:
bool initialized;
vector <string> actionLog;
static int count;
string countString();
};
#endif /* LOG_H_ */
ui.cppp
/*
* Created on: Apr 26, 2011
* ui.cpp
* Author: wjc
*/
#include <iostream>
#include <vector>
#include <sstream>
#include "filedaemon.h"
#include "vsystem.h"
#include "customio.h"
#include "building.h"
#include "log.h"
using namespace std;
bool shouldexit = false;
bool back = false;
int selection;
void addShape();
void modifyVars();
struct getVarResult {
var result;
int status;
};
Log actionLog;
var novar = {"ERROR", -1, Reserved};
getVarResult getVar(int type);
void viewBuilding();
int runUI(){
while (!shouldexit){
cout<<"Please select an item from the list below and press Enter:"<<endl;
const int mmenuLength = 2;
string mmenuOptions[2] = {"Create a new document","Quit"};
for (int i=0; i<2; i++){
cout<<i+1<<": "<<mmenuOptions[i]<<endl;
} cout<<endl;
selection = getMenuItem(1,mmenuLength);
if (selection == mmenuLength) return 0; // Quit if it's the last one
cout<<"Enter a name for your building:"<<endl;
string buildingTitle;
getline(cin, buildingTitle);
Building b(buildingTitle);
actionLog += "New building " + buildingTitle + " created.";
const int bmenuLength = 5;
string bmenuOptions[5] = {"Add shape","Modify variables","View building","View log","Quit"};
for (int i=0; i<bmenuLength; i++){
cout<<i+1<<": "<<bmenuOptions[i]<<endl;
} cout<<endl;
selection = getMenuItem(1,bmenuLength);
switch (selection){
case 1:
// Add a shape
break;
case 2:
modifyVars();
break;
case 3:
// View building
break;
case 4:
{
parsedLog parsed = actionLog.getLog("\n");
if (parsed.status == SUCCESS) {
cout<<"The following contains the contents of your action log."<<endl;
cout<<parsed.result<<endl<<endl;
} else {
cout<<"Somehow your log is not initialized."<<endl<<endl;
}
}
break;
case 5:
shouldexit = true;
break;
default:
cout<<"You entered a number greater than "<<bmenuLength<<" or less than 1. How you did this is a mystery."<<endl<<"[ Press Enter to exit ]"<<endl;
string temp;
getline(cin, temp);
return 0;
}
// The following commented-out block is a
// test of the variable storing system.
// It will not be used in any final products.
/*cout << " Variable Systems Test "<<endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
cout << endl;
cout<<"Enter a variable name:"<<endl;
string varname;
cin>>varname;
cout<<"Enter a value (A FLOAT!):"<<endl;
float value;
cin>>value;
cout<<"Checking to see if "<<varname<<" exists."<<endl;
bool alreadythere = checkVar(varname);
alreadythere ? cout<<"It exists!"<<endl : cout<<"It doesn't exist."<<endl;
if (alreadythere){
cout<<"Changing variable. Function returned "<<changeVar(varname, value)<<endl;
} else {
cout<<"Setting variable. Function returned "<<addVar(varname, value)<<endl;
}
cout<<"Enter a variable to check:"<<endl;
string varcheck;
cin>>varcheck;
fetchResult result = fetchVar(varcheck);
if(! result.good){
cout<<"Variable \""<<varcheck<<"\" doesn't exist!"<<endl;
} else {
cout<<"Variable \""<<varcheck<<"\" is equal to "<<result.result<<endl;
}
cout<<getVarList("\n","\t")<<endl;
string exitstr;
getch;*/
}
return 0;
}
void modifyVars(){
while (! back){
cout<<"These are your defined variables."<<endl;
cout<<"Reserved variables have an asterisk preceding them."<<endl;
vector <var> vars = getVarList();
for (unsigned int i = 0; i<vars.size(); i++){
cout<<endl;
vars[i].reserved ? cout<<" * " : cout<<" ";
cout << vars[i].name<<" = ";
cout<<fixed<<vars[i].value;
}cout<<endl;
cout<<"What would you like to do?"<<endl;
string varMenuOptions[4] = {"Add a variable","Change a variable","Remove a variable","Go back"};
for (int i = 0; i<4; i++){
cout<<i+1<<". "<<varMenuOptions[i]<<endl;
} cout<<endl;
selection = getMenuItem(1,3);
switch(selection){
case 1: // Add variable
{
getVarResult gvr = getVar(ADD);
if (gvr.status == SUCCESS)
addVar(gvr.result.name, gvr.result.value, UserDefined);
break;
}
case 2: // Change variable
{
getVarResult gvr = getVar(CHANGE);
if (gvr.status == SUCCESS)
changeVar(gvr.result.name, gvr.result.value);
break;
} // switch (selection)
} // while (!back)
}
}
getVarResult getVar(int type){
getVarResult finalResult;
getVarResult invalidType;
getVarResult cancelled;
invalidType.result = novar;
invalidType.status = INVALID_TYPE;
cancelled.result = novar;
cancelled.status = USER_CANCELLED;
if (type != ADD && type != CHANGE) return invalidType;
bool usercancelled = false;
bool nameOK = true;
bool varIsReserved = false;
string varName;
do {
switch(type){
case ADD:
if (!nameOK) cout<<"That variable already exists."<<endl;
break;
case CHANGE:
if (!nameOK) cout<<"That variable has not yet been created."<<endl;
if (varIsReserved) cout<<"That variable is used by the system and cannot be changed."<<endl;
break;
}
cout<<"Enter the variable's name, or \"BACK\": "; varName = getString(1,16);
if (varName == "BACK"){
usercancelled = true;
break;
}
fetchResult testExist = fetchVar(varName);
switch(type){
case ADD:
nameOK = !testExist.good;
break;
case CHANGE:
nameOK = testExist.good;
varIsReserved = testExist.reserved;
break;
default:
cout << "Function error - int type seems to have changed since user called getVar(int type)."<<endl;
cout << "[ Press Enter to exit]"<<endl;
string temp;
getline(cin, temp);
return invalidType;
}
} while (! nameOK || varIsReserved);
finalResult.result.name = varName;
if (usercancelled) return cancelled;
bool valueOK = true;
float numValue;
do {
if (! valueOK) cout<<"That doesn't seem to be a valid positive number.";
cout<<"Enter the new value, or \"COPY\" to copy a variable, or \"BACK\":"<<endl;
string value = getString();
/*
* If "BACK" then break do-while(! valueOK)
*/
if (value == "BACK"){
usercancelled = true;
break;
}
if(value == "COPY"){
string copyVar;
fetchResult varContents;
bool copyOK = true;
do {
if (!copyOK) cout<<"That variable does not exist. Note that names are case-sensitive."<<endl;
cout<<"Enter the variable to copy, \"VIEW\" to view all, or \"BACK\":"<<endl;
/*
* If "BACK" then break do-while(! valueOK)
*/
if (value == "BACK"){
usercancelled = true;
break;
}
copyVar = getString(1,8);
if (copyVar == "VIEW") {
cout<<"Your current variables are as follows:"<<endl;
vector <var> vars = getVarList();
for (unsigned int i = 0; i<vars.size(); i++){
cout<<endl;
vars[i].reserved ? cout<<" * " : cout<<" ";
cout << vars[i].name<<" = ";
cout<<fixed<<vars[i].value;
}cout<<endl;
} else {
varContents = fetchVar(copyVar);
copyOK = varContents.good;
numValue = varContents.result;
}
} while (copyVar == "VIEW" || ! copyOK);
} else {
// This code converts from string to number safely.
stringstream testStream(value);
if (! (testStream >> numValue))
valueOK = false;
}
if (! usercancelled) break;
} while (! valueOK);
finalResult.result.value = numValue;
if (usercancelled) return cancelled;
finalResult.status = SUCCESS;
return finalResult;
}
ui.h
/*
* ui.h
*
* Created on: Apr 26, 2011
* Author: wjc
*/
#ifndef UI_H_
#define UI_H_
#include "vsystem.h"
int runUI();
void addShape();
void modifyVars();
struct getVarResult {
var result;
int status;
};
getVarResult getVar(int type);
void viewBuilding();
#endif /* UI_H_ */
building.h
/*
* building.h
*
* Created on: Apr 30, 2011
* Author: wjc
*/
#ifndef BUILDING_H_
#define BUILDING_H_
#include <string>
#include <vector>
#include "consts.h"
#include "solid_type.h"
using namespace std;
struct dimension {
bool exists;
float value;
};
class Solid {
public:
string name;
string comment;
solid_type type;
bool positive;
dimension dim1; // Radius, width, or side length
dimension dim2; // Height, number of sides, or fraction of sphere_over_n
dimension dim3; // Width - only for prism_rect, pyrm_rect and tprym_rect
Solid ();
Solid (bool pos);
Solid (string setName, string setComment, solid_type setType, bool setPos, dimension setDim1,
dimension setDim2, dimension setDim3);
~Solid();
int countShapes();
int howMany();
private:
static int count;
};
class Building {
private:
string name;
vector <Solid> components;
public:
Building(string text);
void setName(string text);
string getName();
vector <Solid> addComponent(Solid component);
};
#endif /* BUILDING_H_ */
building.cpp
/*
* building.cpp
*
* Created on: May 1, 2011
* Author: wjc
*/
#include <string>
#include <vector>
#include "consts.h"
#include "solid_type.h"
using namespace std;
struct dimension {
bool exists;
float value;
};
class Solid{
public:
string name; // So the user can look at the log
string comment; // Expanded version of name, again for the log
solid_type type; // Determines the type of Solid
bool positive; // Positive = addition; negative = subtraction
dimension dim1; // Radius, width, or side length
dimension dim2; // Height, number of sides, or fraction of sphere_over_n
dimension dim3; // Width - only for prism_rect, pyrm_rect and tprym_rect
Solid(){
count++;
}
Solid(bool setPos){
count++;
positive = setPos;
}
Solid (string setName, string setComment, solid_type setType, bool setPos, dimension setDim1,
dimension setDim2, dimension setDim3){
count++;
name = setName;
comment = setComment;
type = setType;
positive = setPos;
dim1 = setDim1;
dim2 = setDim2;
dim3 = setDim3;
}
~Solid(){
count--;
}
int howMany(){
return count;
}
int countSolids(){
return howMany();
}
private:
static int count; // Number of Solids in existence
};
class Building {
private:
string name;
vector <Solid> components;
public:
Building (string text){
setName(text);
}
void setName(string text) {
name = text;
}
string getName(){
return name;
}
vector <Solid> addComponent(Solid component){
components.push_back(component);
return components;
}
};
My complete code is available at FileDropper, although I know most people won't want to unpack a zip, so I put the relevant ones above.
Any help would be greatly appreciated!
Running Eclipse Helios with G++ 4.2.1 on Mac OS X Snow Leopard 10.6.7.
The complete linking command is:
g++ -o "BuildingGenerator" ./src/bgmath.o ./src/building.o ./src/customio.o \
./src/filedaemon.o ./src/log.o ./src/main.o ./src/ui.o ./src/vsystem.o
So it looks like it is linking all the necessary files. Also, the (header) files are all #include'd where needed.
Problem discovered; outline fix created...
Code received, unpacked, and compiled; the linker produces essentially the same error that you're seeing...
Osiris-8 JL: make bgmath.o building.o customio.o filedaemon.o log.o main.o ui.o vsystem.o
g++ -c -o bgmath.o bgmath.cpp
g++ -c -o building.o building.cpp
g++ -c -o customio.o customio.cpp
g++ -c -o filedaemon.o filedaemon.cpp
g++ -c -o log.o log.cpp
g++ -c -o main.o main.cpp
g++ -c -o ui.o ui.cpp
g++ -c -o vsystem.o vsystem.cpp
Osiris-8 JL: g++ -o cmd *.o
Undefined symbols for architecture x86_64:
"Building::Building(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
runUI() in ui.o
"Log::operator+=(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
runUI() in ui.o
"Log::getLog(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
runUI() in ui.o
"Log::Log()", referenced from:
__static_initialization_and_destruction_0(int, int) in ui.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Osiris-8 JL:
This is on MacOS X 10.6.7 with GCC 4.6.0 (which I compiled). Since it is substantially the same as what you got, the version of G++ is essentially irrelevant (but not wholly; the c++filt distributed along with GCC 4.2.1 by Apple does not recognize the 'nm' output you provided, even after cleaning the BOM out of the file).
OK - several grey hairs later - I know roughly what is going on.
When I run nm log.o, it said 'nm: no name list'. That's because it is, in fact, an empty object file. And it is an empty object file because all the code is in a class declaration, but is never used so there is no object code in the file, and no functions.
I had a suspicion that it was something to do with definitions - I had not guessed it was as completely 'no defined functions' as it is.
So, how do we fix it?
The first problem is that you have log.cpp but it does not include log.h. This is a strong indicator of problems. The header defines the public interface to the class(es) defined in the source, and the only (sane) way of ensuring they are in agreement is to include the header in the source file. In fact, it is best to include the header first so that you can be sure it is self-contained (can be used in any source module).
When we do that, we find immediately that log.h is not self-contained -- it needs #include <vector> too. When that's fixed, we find that you cannot compile log.cpp successfully:
g++ -c -o log.o log.cpp
log.cpp:12:8: error: redefinition of ‘struct logResult’
log.h:15:8: error: previous definition of ‘struct logResult’
log.cpp:17:8: error: redefinition of ‘struct parsedLog’
log.h:20:8: error: previous definition of ‘struct parsedLog’
log.cpp:22:7: error: redefinition of ‘class Log’
log.h:25:7: error: previous definition of ‘class Log’
make: *** [log.o] Error 1
The two structures are declared in the header, so they should not be redeclared in the source. The function definitions must be prefixed with Log:: and removed from the 'class Log { ... }' braces. There is then routine clean up of the compilation errors, leading to:
log.h
#ifndef LOG_H_
#define LOG_H_
#include <vector>
#include <string>
using namespace std;
struct logResult {
vector <string> result;
int status;
};
struct parsedLog {
string result;
int status;
};
class Log {
public:
int initialize();
int initialize(string text);
Log ();
Log (string text);
bool isInitialized();
int add(string text);
int operator+= (string text);
int clearLog (bool init = true);
logResult getLog();
parsedLog getLog(string delim);
private:
bool initialized;
vector <string> actionLog;
};
#endif /* LOG_H_ */
log.cpp
#include "log.h"
#include "consts.h"
using namespace std;
int Log::initialize(){
if (initialized) return ALREADY_INIT;
actionLog.push_back("*** Log initialized ***");
initialized = true;
return SUCCESS;
}
int Log::initialize(string text){
if (initialized) return ALREADY_INIT;
// otherwise...
initialize();
actionLog.push_back("Initialization message: "+text);
return SUCCESS;
}
Log::Log (){
initialize();
}
Log::Log (string text){
initialize(text);
}
bool Log::isInitialized(){
return initialized;
}
int Log::add(string text){
if (!initialized) return NOT_INIT;
actionLog.push_back(text);
return SUCCESS;
}
int Log::operator+= (string text){
return add(text);
}
int Log::clearLog(bool init){
if (!initialized) return NOT_INIT;
//delete actionLog;
initialized = false;
if (init) return initialize();
// Else
return SUCCESS;
}
logResult Log::getLog(){
if (!initialized){
logResult final;
final.status = NOT_INIT;
return final;
} else {
logResult final;
final.result = actionLog;
final.status = SUCCESS;
return final;
}
}
parsedLog Log::getLog(string delim){
if (!initialized){
parsedLog final;
final.status = NOT_INIT;
return final;
} else {
parsedLog final;
string logString;
for (unsigned int i; i<actionLog.size()-1; i++){
logString += actionLog[i];
logString += delim;
}
logString += actionLog.back();
final.result = logString;
final.status = SUCCESS;
return final;
}
}
These files compile cleanly to generate log.o containing some useful functions. The link failure after that is:
Undefined symbols for architecture x86_64:
"Building::Building(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
runUI() in ui.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Fixing that is left as an exercise for you - but I believe analogous changes are needed in building.h and building.cpp.
There are still some issues that should be resolved. In particular, headers should not usually do using namespace std; as that is an unwarranted intrusion into other programmer's control over the namespaces they use. See Using namespace in C++ headers for a recent discussion of this issue.