Thread: Still Vectorstuff...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    30

    Unhappy Still Vectorstuff...

    Hi,

    i still got some Probs with my Code :-(

    Code:

    Code:
    #include <vector>
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    struct SimpVec
    {
    	float coordx;
    	float coordy;
    	float coordz;
    };
    
    
    int main()
    {
    
    vector<SimpVec> alphaVector;
    for( int i=0; i < 10; i++ )
      alphaVector.push_back( i + 65 );
    
    // Display the vector
    for( theIterator = alphaVector.begin(); theIterator != alphaVector.end(); theIterator++ )
      cout << *theIterator;
    
    return 0;
    }
    .

    Here are some compiling errors: (follow compileerrors are cut out)

    Compiling...
    test.cpp
    c:\programme\vtk40\cylinder\test.cpp(21) : error C2664: 'push_back' : Converting of Parameter 1 from 'int' to 'const struct SimpVec &' not possible.
    c:\programme\vtk40\cylinder\test.cpp(27) : error C2440: '=' : 'struct SimpVec *' cannot be converted to 'int'
    c:\programme\vtk40\cylinder\test.cpp(27) : error C2446: '!=' : No Conversion from 'struct SimpVec *' to 'int'
    Error executing cl.exe.

    Anyone knowing the problem? I am new to this stuff, and i need to make it, even this isn't real beginnerstuff.

    Thanks in advance!
    Jan



    &#91;code]&#91;/code]tagged by Salem

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    Pushback is expecting a SimpVect object. You passed it an int.

    Code:
    SimpVec aVec;
    
    aVec.coordx=7;
    aVec.coordy=8;
    aVec.coordz=9;
    
    alphaVector.push_back(aVec);
    The other errors are similar to this. Define vectors as shown here.
    Best Regards,

    Bonkey

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Structs in C++ can have their own constructors just as classes do. One idea would be to create a constructor for the SimpVec struct and use that constructor in the call to the push_back function.
    Code:
    #include <vector>
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    struct SimpVec
    {
        float coordx;
        float coordy;
        float coordz;
        SimpVec( float x, float y, float z )
        {
            coordx = x;
            coordy = y;
            coordz = z;
        }
    };
    
    
    int main()
    {
    
    vector<SimpVec> alphaVector;
    vector<SimpVec>::iterator theIterator;
    for( int i=0; i < 10; i++ )
      alphaVector.push_back( SimpVec( 1.0, 1.0, 1.0 ) );
    
    // Display the vector
    for( theIterator = alphaVector.begin(); theIterator != alphaVector.end(); theIterator++ )
      cout << (*theIterator).coordx << ' '
            << (*theIterator).coordy << ' '
            << (*theIterator).coordz << endl;
    
    return 0;
    }
    Of course, this would populate the vector with 10 instances of a struct where all the x/y/z values are 1.0 but you should get the idea and be able to modify this easily.
    Last edited by hk_mp5kpdw; 10-29-2002 at 01:18 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    30
    Okay, that example works now.
    I put now the concept into the real Program:
    Code:
    //pcavis.h
    #include <vtkCylinderSource.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkActor.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkXRenderWindowInteractor.h>
    #include <vtkProperty.h>
    #include <vtkCamera.h>
    #include <vtkConeSource.h>
    #include <vtkSphereSource.h>
    #include <vtkGlyph3D.h>
    #include <vtkPoints.h>
    #include <vtkFloatArray.h>
    #include <stdio.h>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    struct SimpVec
    {
      float coordx;
      float coordy;
      float coordz;
    };
    
    vector<SimpVec> vecs;

    Code:
    //main.C
    #include "pcavis.h"
    
    #define RESOLUTION 20
    #define POINTRADIUS 0.05
    
    
    void datareader();
    //void pcavis2();
    
    int main(int argc, char *argv[])
    {
      datareader();
    }
    Code:
    #include "pcavis.h"
    
    
    //int dimen;
    //void pcavis2(vector<SimpVec> ChargeVec, int anz);
    
    void datareader()
    {
        int i = 0;
        int anz = 0;
        SimpVec einVektor;
        int real; // number of right vectors, length of vecs
        int err;  // number of errorous vectors
        einVektor.coordx = 0.0;     // this is a dummy assigment to force the compiler to link floating point support
    
        FILE *pfile = fopen("pca.raw", "r");
        if (!pfile) {
            printf("pcaFile not found!\n");
            i = 1;
        }
        else {
            while (fscanf(pfile, "%f;%f;%f;\n", &einVektor.coordx, &einVektor.coordy, &einVektor.coordz) != EOF) {
                printf("Line %d has the following Vectordata: %f %f %f\n", anz, einVektor.coordx, einVektor.coordy, einVektor.coordz);
                          real = anz - err;          
                          vecs::resize(real);
                          vecs.at(real).coordx = einVektor.coordx;
                          vecs.at(real).coordy = einVektor.coordy;
                          vecs.at(real).coordz = einVektor.coordz;
                          anz++;
                    }
    
            if (!feof(pfile)) {
                printf("Error while reading the line!\n");
                err++;
                anz++;            
            }
            else
                //printf("That are %d Vectors!\n", anz);
                cout << "That are " << anz << " lines with " << real << " gueltigen Vektoren" << endl;
                fclose(pfile);
        }
    }
    Compiling gives me the following Errors:

    Datareader.C: In function `void datareader()':
    Datareader.C:25: parse error before `::'
    Datareader.C:26: no matching function for call to `vector<SimpVec,allocator<SimpVec> >::at (int &)'
    Datareader.C:27: no matching function for call to `vector<SimpVec,allocator<SimpVec> >::at (int &)'
    Datareader.C:28: no matching function for call to `vector<SimpVec,allocator<SimpVec> >::at (int &)'


    That vector called vecs is defined in pcavis.h so it won't be deleted after the datareadingpart is done, so i can give it to the vtk-stuff which will be called in main.C

    edit:
    There is a logical 'bug' preventing from doing what i want, but it is NOT affecting the compilingerror

    Thanks in advance!
    Jan
    Last edited by Jan79; 10-29-2002 at 02:06 PM.

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    You can use the [] operator instead of at. It is faster, but does not provide checking of the bounds.

    Also, this:
    vecs::resize(real);
    should be this
    vecs.resize(real);

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    30
    I got it compiled but getting segmentationfault after first line is being read:

    Code:
    #include "pcavis.h"
    
    vector<SimpVec> vecs;
    
    //int dimen;
    //void pcavis2(vector<SimpVec> ChargeVec, int anz);
    
    void datareader()
    {
        int i = 0;
        int anz = 0;
        SimpVec einVektor;
        int real; // number of right vectors, length of vecs
        int err;  // number of errorous vectors
        einVektor.coordx = 0.0;     // this is a dummy assigment to force the compiler to link floating point support
    
        FILE *pfile = fopen("pca.raw", "r");
        if (!pfile) {
            printf("pcaFile not found!\n");
            i = 1;
        }
        else {
            while (fscanf(pfile, "%f;%f;%f;\n", &einVektor.coordx, &einVektor.coordy, &einVektor.coordz) != EOF) {
                printf("Line %d has the following Vectordata: %f %f %f\n", anz, einVektor.coordx, einVektor.coordy, einVektor.coordz);
                          //real = anz - err;          
                          vecs.resize(anz);
                          vecs[anz].coordx = einVektor.coordx;
                          vecs[anz].coordy = einVektor.coordy;
                          vecs[anz].coordz = einVektor.coordz;
                          anz++;
                    }
    
            if (!feof(pfile)) {
                printf("Error while reading the file!\n");
                //err++;
                //anz++;
                exit(2);            
            }
            else
                //printf("That are %d Vectors!\n", anz);
                cout << "That are " << anz << " lines with " << real << " gueltigen Vektoren" << endl;
                fclose(pfile);
        }
    
    }
    Content of the pca.raw file:
    0.2;0.5;0.3;
    0.7;0.1;0.6;
    0.3;0.9;0.7;
    0.5;0.3;0.1;

    This is the result:
    jdrexhag@germanicus:/vol/pgml/share/visu/jan/pcavis3> visu
    Line 0 has the following Vectordata: 0.200000 0.500000 0.300000
    Segmentation fault


    Where is the error? I resize the vecs each time the line is being read successfully, and adding the linevalue into the vecs.

    Thanks in advance!
    Jan

    edit: i found some other bug, and replaced the code with the actual one
    Last edited by Jan79; 10-29-2002 at 02:29 PM.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Perhaps... when you first get to the resize function, anz is equal to 0 and I'm sure you don't mean to say resize(0). What you probably want is resize(anz+1). Then again, if you simply say vecs.push_back( einVektor ) at that point in the code, then you can get rid of all the resize and vecs[anz].coord_ = einVektor.coord_ related code.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    30
    very well, i guess this is my last Problem to get the Program working:
    I thought it is possible to declare with
    Code:
    vector<SimpVec> vecs;
    in the Headerfile this global, so one file is filling the vector<SimpVec> and another file is reading and visualising that vector<SimpVec>.

    But when i try to compile it i get the following error:

    Datareader.o(.bss+0x0): multiple definition of `vecs'
    main.o(.bss+0x0): first defined here
    Pcavis2.o(.bss+0x0): multiple definition of `vecs'
    main.o(.bss+0x0): first defined here
    collect2: ld returned 1 exit status
    make: *** [all] Error 1

    It sounds like the compiler doesn't like that global declared vector<SimpVec>, how can i fix it?

    edit: None of the files have an additional delcaration of vecs, it is only declared in the headerfile.

    Thanks in advance!
    Jan

  9. #9
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Is that header file included in more than 1 of your project files?

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    30
    the project has 3 files and is included in each file once, so it is global, isn't that the usual way?

Popular pages Recent additions subscribe to a feed