About the initialization problem of declaring a vector as a global variable in QT

Source: Internet
Author: User
Tags constructor

A simple int-type global variable is described earlier, but for a more complex vector type, the above method can be used for reference, but the problem of initialization should be different.

Global variables: Declared in the class of the header file, initialized outside the function body of the CPP file. This is the basic routine.

The static variables of the class must be initialized outside of the class, you know.

A little more specific.

STLView.h

class Stlview {public:stlview ();    Declares the constructor bool Readstl (const QString &fname);    Read STL data qvector<mytriangle> Trianglevector;  Set of triangular polygons static int splitstring (const QString &str, Char split, qvector<qstring>& Strarray); is a static member function double trianglezmax,trianglezmin;
    Defines the z-coordinate maximum and minimum values for all triangular patches static bool Comp (const mytriangle &a, const mytriangle &b); Sorting functions, sorted by mytriangle.zmin values; the larger the qvector ordinal, the larger the zmin, the higher the ascending order, the static member function that must be written here//if the zmin of triangles A and B are the same, then the order of A and B in the STL file, in front of it,
Finally in the qvector inside row behind//double read (); MyPoint Linedir,testpoint; Define the direction of the two-point line, test point qvector<crosspoint> crosspointvector; Intersection container static qvector<qpointf> vector;
    Set the contour two-dimensional point information, define a vector to store qpoint type of point static qpointf points;  static int A; Define global variables double zlevel; Defines the value of the Z-plane double Dist (mypoint P1, MyPoint P2); Calculate two points away from bool between (MyPoint P1, MyPoint P2, MyPoint P3);
Determine if the point3 is between Point1 and Point2}; 

STLView.cpp

#include "STLView.h"
#include <QFile>
#include <QTextStream>
#include <QDebug>
# Include <qtalgorithms>//data sorting
#include <QList>

int stlview::a = 11111;//int type initialization

qpointf Stlview::p oint = qpointf (); Vector type initialization
qvector<qpointf> stlview::vector = qvector<qpointf> ();


Stlview::stlview ()//default constructor, declaration with array must have default constructor
{

}



One problem has to be taken seriously:
When assigning the initial value to a vector of the qpointf type, the general vector assignment is append,push_back, but the two functions must be used inside the function;
External to the global variable vector assignment, obviously append,push_back is not useful, so we call the vector's constructor, and then in the class's constructor insert or assign a value.
Finally, thank you Dalian (27754587) Brother Wei

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.