Trying to create a vector using a structure definition as the basis in VC++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 38

Thread: Trying to create a vector using a structure definition as the basis in VC++

  1. #1
    Join Date
    Feb 2005
    Posts
    34

    Trying to create a vector using a structure definition as the basis in VC++

    I was trying to apply what is here (as someone who writes rarely and has to relearn everything each time):

    http://www.daniweb.com/software-deve...ctor-of-struct

    I'm using a header file to define the structure:

    #ifndef EINSTEIN_H
    #define EINSTEIN_H

    #include <stdio.h>
    #include <vector>



    struct SizeAll{
    double year;
    double R;
    SizeAll() { year = 0; R = 0; }
    };



    int write_to_file(int count, struct SizeAll *data, char const

    *fileName)
    {
    FILE* fp;
    fp = fopen(fileName, "w");
    if (fp == NULL) return -1;
    while (count-- > 0) {
    fprintf(fp, "%d,%d\n", data->year, data->R);
    //check for errors writing to file
    if (ferror(fp)) return -1;
    }
    fclose(fp);
    return 0;
    }
    #endif


    I've loaded the following in the "main.cpp" equivalent. (Not called "main.cpp", but serves the same purpose.)
    #include "stdafx.h"
    #include <vector>
    #include <stdio.h>
    #include "Einstein.h"
    #include <math.h>
    #include "Form1.h"

    The code that actually seeks to generate the vector using the structure definition follows:


    int counter,year,retval,count;
    vector<SizeAll> Universe;
    Universe.resize(5);
    vector<SizeAll> *heretis;
    char const NameFile[]="D:\\Friedout.csv";

    heretis=&Universe;
    Universe.R = double(366);
    Universe.year = double(188);
    for ( counter = 15000 ; counter > 99 ; counter-- )
    {
    Universe.year =double(counter);
    Universe.R = double(counter*2);
    if (counter==100||counter ==2500||counter==5000||counter==7500||counter==10000||counter==12500||counter==15000)
    {
    retval=write_to_file(1, heretis, NameFile);
    if (retval ==-1)
    {
    break;
    }
    }
    }


    This creates quite a mess. It seems that somehow the "vector" declaration isn't working as the referenced web link seems to suggest that it should. I presume that, as usual, clearing one real error will eliminate the cascade of errors the first real error produces. Can anyone clear this up for me? Why won't VC++ accept the "vector" identifier? Thank you.

    The error messages that follow the build attempt are:

    Friedman.cpp
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Einstein.h(22): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    C:\Program Files\Microsoft Visual Studio 10.0\VC\include\stdio.h(234) : see declaration of 'fopen'
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(80): warning C4067: unexpected tokens following preprocessor directive - expected a newline
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(82): error C2065: 'vector' : undeclared identifier
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(82): error C2275: 'SizeAll' : illegal use of this type as an expression
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Einstein.h(9) : see declaration of 'SizeAll'
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(82): error C2065: 'Universe' : undeclared identifier
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(83): error C2065: 'Universe' : undeclared identifier
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(83): error C2228: left of '.resize' must have class/struct/union
    type is ''unknown-type''
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(84): error C2065: 'vector' : undeclared identifier
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(84): error C2275: 'SizeAll' : illegal use of this type as an expression
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Einstein.h(9) : see declaration of 'SizeAll'
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(84): error C2065: 'heretis' : undeclared identifier
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(87): error C2065: 'heretis' : undeclared identifier
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(87): error C2065: 'Universe' : undeclared identifier
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(88): error C2065: 'Universe' : undeclared identifier
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(88): error C2228: left of '.R' must have class/struct/union
    type is ''unknown-type''
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(89): error C2065: 'Universe' : undeclared identifier
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(89): error C2228: left of '.year' must have class/struct/union
    type is ''unknown-type''
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(92): error C2065: 'Universe' : undeclared identifier
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(92): error C2228: left of '.year' must have class/struct/union
    type is ''unknown-type''
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(93): error C2065: 'Universe' : undeclared identifier
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(93): error C2228: left of '.R' must have class/struct/union
    type is ''unknown-type''
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(96): error C2065: 'heretis' : undeclared identifier
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  2. #2
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Trying to create a vector using a structure definition as the basis in VC++

    Quote Originally Posted by Protocol View Post
    I was trying to apply what is here (as someone who writes rarely and has to relearn everything each time):

    http://www.daniweb.com/software-deve...ctor-of-struct

    I'm using a header file to define the structure:
    Please use code tags when posting code. You were told how to do that in other threads. The code you posted is unreadable without them.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,746

    Re: Trying to create a vector using a structure definition as the basis in VC++

    Vector is part of the std namespace. Try inserting after your includes:

    Code:
    using namespace std;

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,204

    Re: Trying to create a vector using a structure definition as the basis in VC++

    You have code that's not inside a function.

  5. #5
    Join Date
    Feb 2005
    Posts
    34

    Re: Trying to create a vector using a structure definition as the basis in VC++

    Paul,

    Would you please give me the information I need to use these "code tags"? I'm not used to employing them. I'm using Chrome as a browser, if that has any effect. Thank you.

  6. #6
    Join Date
    Feb 2005
    Posts
    34

    Re: Trying to create a vector using a structure definition as the basis in VC++

    2kaud,

    Thank you for the useful input. I've applied it, and it does seem to at least change the error messages I'm getting.

    I have some additional questions for you, if you happen to be able to favor me with a response. Thank you.

    1. What does "using namespace std;" do? I ask, because this is a "managed code" project, and the managed code that produces the form for Windows under VC++ 2010 is also referencing a DIFFERENT namespace. The VC++ managed code namespace reference is right where you told me to put the std namespace reference. It is in the "main.cpp" equivalent, and it looks like:

    (Imagine this is in "code tags", please...)

    // Friedman.cpp : main project file.

    #include "stdafx.h"
    #include <vector>
    #include <stdio.h>
    using namespace std;
    #include "Einstein.h"
    #include <math.h>
    #include "Form1.h"
    using namespace Friedman;

    As you can see, it references the name of the "solution/project", which is Friedman, as the "namespace". I don't understand "namespace" as a reference, so I don't know if I'm stepping on toes with the "std namespace" reference.

    2. I'm getting a new batch of errors, but they seem less obstinate than the first. Honestly, I haven't given them a great deal of thought yet, since I'm a little nervous regarding my lack of understanding of what I've actually done by incorporating two "namespace" references in the "main.cpp" equivalent.

    ERRORS from "build" attempt:

    ------ Build started: Project: Friedman, Configuration: Debug Win32 ------
    Friedman.cpp
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Einstein.h(22): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    C:\Program Files\Microsoft Visual Studio 10.0\VC\include\stdio.h(234) : see declaration of 'fopen'
    d:\documents and settings\del ventruella\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(88): error C2039: 'R' : is not a member of 'std::vector<_Ty>'
    with
    [
    _Ty=SizeAll
    ]
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(89): error C2039: 'year' : is not a member of 'std::vector<_Ty>'
    with
    [
    _Ty=SizeAll
    ]
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(92): error C2039: 'year' : is not a member of 'std::vector<_Ty>'
    with
    [
    _Ty=SizeAll
    ]
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(93): error C2039: 'R' : is not a member of 'std::vector<_Ty>'
    with
    [
    _Ty=SizeAll
    ]
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(96): error C2664: 'write_to_file' : cannot convert parameter 2 from 'std::vector<_Ty> *' to 'SizeAll *'
    with
    [
    _Ty=SizeAll
    ]
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  7. #7
    Join Date
    Feb 2005
    Posts
    34

    Re: Trying to create a vector using a structure definition as the basis in VC++

    2kaud,

    Thank you for the useful input. I've applied it, and it does seem to at least change the error messages I'm getting.

    I have some additional questions for you, if you happen to be able to favor me with a response. Thank you.

    1. What does "using namespace std;" do? I ask, because this is a "managed code" project, and the managed code that produces the form for Windows under VC++ 2010 is also referencing a DIFFERENT namespace. The VC++ managed code namespace reference is right where you told me to put the std namespace reference. It is in the "main.cpp" equivalent, and it looks like:

    (Imagine this is in "code tags", please...)

    // Friedman.cpp : main project file.

    #include "stdafx.h"
    #include <vector>
    #include <stdio.h>
    using namespace std;
    #include "Einstein.h"
    #include <math.h>
    #include "Form1.h"
    using namespace Friedman;

    As you can see, it references the name of the "solution/project", which is Friedman, as the "namespace". I don't understand "namespace" as a reference, so I don't know if I'm stepping on toes with the "std namespace" reference.

    2. I'm getting a new batch of errors, but they seem less obstinate than the first. Honestly, I haven't given them a great deal of thought yet, since I'm a little nervous regarding my lack of understanding of what I've actually done by incorporating two "namespace" references in the "main.cpp" equivalent.

    ERRORS from "build" attempt:

    ------ Build started: Project: Friedman, Configuration: Debug Win32 ------
    Friedman.cpp
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Einstein.h(22): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    C:\Program Files\Microsoft Visual Studio 10.0\VC\include\stdio.h(234) : see declaration of 'fopen'
    d:\documents and settings\del ventruella\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(88): error C2039: 'R' : is not a member of 'std::vector<_Ty>'
    with
    [
    _Ty=SizeAll
    ]
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(89): error C2039: 'year' : is not a member of 'std::vector<_Ty>'
    with
    [
    _Ty=SizeAll
    ]
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(92): error C2039: 'year' : is not a member of 'std::vector<_Ty>'
    with
    [
    _Ty=SizeAll
    ]
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(93): error C2039: 'R' : is not a member of 'std::vector<_Ty>'
    with
    [
    _Ty=SizeAll
    ]
    d:\documents and settings\xx\my documents\visual studio 2010\projects\friedman\friedman\Form1.h(96): error C2664: 'write_to_file' : cannot convert parameter 2 from 'std::vector<_Ty> *' to 'SizeAll *'
    with
    [
    _Ty=SizeAll
    ]
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


    Thank you for your understanding regarding the appearance of some of this data.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Trying to create a vector using a structure definition as the basis in VC++

    Quote Originally Posted by Protocol View Post
    1. What does "using namespace std;" do?
    Namespaces are fundamental to C++. The very first C++ program you write uses this.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
       cout << "Hello World";
    }
    This is a basic "Hello World" program in C++. Without the namespace, the code will not compile due to "cout" not being found.
    (Imagine this is in "code tags", please...)
    Why "imagine"? I already stated in another thread how to use code tags. Once again:

    [code]
    Your code goes here
    [/code]

    Second, are you trying to learn C++ by writing things and seeing what sticks? Well, C++ can't be learned that way. You need to get a book or tutorial and go through the basics of the language. It isn't the same as learning HTML or some other script or script-like language, where all you need is an hour and a cheat sheet to help you out.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Feb 2005
    Posts
    34

    Re: Trying to create a vector using a structure definition as the basis in VC++

    No, I'm not trying to learn C or C++ from scratch, but I've never tried to create a vector of structures before, and haven't written anything in C or C++ for years. Passing a vector of structure to a subroutine in a header file is also new to me. Neither of these topics were specifically covered in the beginner course in "C" that I completed via a local university. In general, I hope to learn more by creating code and building on what I already know, with some help from others who are willing to assist.

    Tutorials typically don't provide detailed guidance for the specific problems I'm now dealing with. The course I completed in "C" certainly didn't. I've already searched the web relative to these matters repeatedly, as I indicated. I'm seeking guidance, from knowledgeable parties, who actually sound and act like they're willing to provide helpful insights. That can help me to move beyond the basics, and overcome what I may have lost over the years away from "C" (such as a facility with pointer variables). Is there some reason you're inclined to seek to deter my effort to improve my grasp of C/C++ by discouraging me from posting on this site? First you insist that "managed" VC++ code has no place in the "VC++ forum". Now I seem to face what is being expressed as a more fundamental issue with my presence here. I certainly have no intent to specifically burden any party, individually, with my questions.

    If anyone has a few moments and no inclination toward focusing resentment at those trying to move beyond the basics, and to achieve a better grasp of certain "basics", I'd love to know what a "namespace" is and how referencing two of them as I've done (please see below) might affect the code I've written. I'd certainly appreciate any insightful response in that regard.


    To keep things up to date (and formatted in conformance with local standards), the code I'm using to seek to create and pass a vector containing structured data elements follows (and is contained within a "form" in the code I've written):


    Code:
    int counter,year,retval,count=0;
        vector<SizeAll> Universe;
    	Universe.resize(5);
    	vector<SizeAll> *heretis;
        char const NameFile[]="D:\\Friedout.csv";
    	
    	heretis=&Universe;
        Universe[0].R = double(366);
        Universe[0].year = double(188);
    		for ( counter = 15000 ; counter > 99 ; counter-- )
    		{
            
    		if (counter==100||counter ==2500||counter==5000||counter==7500||counter==10000||counter==12500||counter==15000)
    		{
    		
    		Universe[count].year =double(counter);
    		Universe[count].R = double(counter*2);
    		count = count + 1;
            retval=write_to_file(1, heretis, NameFile);
    		if (retval ==-1) 
    			{
    			break;
    			}
    		}
    		}

    The header file that contains the structure definition and the subroutine that is meant to create a file containing data passed from the preceding code is now defined as:

    Code:
    #ifndef EINSTEIN_H
    #define EINSTEIN_H
    
    #include <stdio.h>
    #include <vector>
    using namespace std;
    
    
    struct SizeAll{
            double year;
            double R;
    		SizeAll() { year = 0; R = 0; }
            };
    
     
    
    int write_to_file(int count, vector<SizeAll> *data, char const
    
    *fileName)
    {
      FILE* fp;
      fp = fopen(fileName, "w");
      if (fp == NULL) return -1;
      while (count-- > 0) {
        fprintf(fp, "%d,%d\n", data[0].year, data[0].R);
    	//check for errors writing to file
    	if (ferror(fp)) return -1;
      }
      fclose(fp);
      return 0;
    }
    #endif
    The header file "include" statements remain as follows (for now, pending further insight into "namespaces":

    Code:
    // Friedman.cpp : main project file.
    
    #include "stdafx.h"
    #include <vector>
    #include <stdio.h>
    using namespace std;
    #include "Einstein.h"
    #include <math.h>
    
    
    #include "Form1.h"
    using namespace Friedman;
    My thanks to anyone who cares to take a look and make a response. At this time, it appears that the "build" is generating errors suggesting that the vector of structured data that I'm passing with a pointer is not being properly referenced. (It is not recognizing the structure variables because they aren't associated with indices and are not defined in the "class". Simply adding vector indices doesn't seem to solve the problem. This leaves me to question whether passing the pointer variable is enough to permit me to access the vector of structured data in the Einstein.h header file subroutine. Is there a way to deal with this? Originally, I referenced the data using pointers in the Einstein.h header file subroutine. Now the "build" is saying it can't do this with the vector of structured data, and asks if I wish to use a "." to reference the structure elements.

    Have a nice day!
    Last edited by Protocol; January 25th, 2013 at 02:48 PM.

  10. #10
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,746

    Re: Trying to create a vector using a structure definition as the basis in VC++

    The using directive allows the names in a namespace to be used without the namespace-name as an explicit qualifier. If a name is defined in a namespace then it must be referenced via that namespace name. The namespace of the c++ standard library is std. Therefore all references to names from the standard library (such as string, vector) need to be preceeded by std:: such as
    Code:
    #include <vector>
    
    int main()
    {
       std::vector<int> intvec;
       return 0;
    }
    The using directive means that the explicit namespace need not be specified as

    Code:
    #include <vector>
    using namespace std;
    
    int main()
    {
       vector<int> intvec;
       return 0;
    }
    If Friedman is the name of the "solution/project" then this is not a namespace and hence

    using namespace Friedman;

    is incorrect and should not be included.

    You need to get a book or tutorial and go through the basics of the language. It isn't the same as learning HTML or some other script or script-like language, where all you need is an hour and a cheat sheet to help you out.
    Paul is absolutely correct. c++ is a very complex language. You just can't 'wing it' as you go along and hope for the best. You must start with the simple basics and only advance one step when you understand the current step.

    Incidentially, you mention that this is a 'managed code' project. This forum is not for managed code. There is another forum which deals with managed code. People on this forum can't help with managed code issues.

  11. #11
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Trying to create a vector using a structure definition as the basis in VC++

    Quote Originally Posted by Protocol View Post
    No, I'm not trying to learn C or C++ from scratch,
    Which is the whole problem.

    How are you going to learn the advanced topics if you don't know the basics, such as namespaces? You can't cherry-pick certain topics in C++ and expect to get anything useful done. You have to know the basics before going on to do anything else in C++.
    Passing a vector of structure to a subroutine in a header file is also new to me.
    Passing vector is no different than passing any other parameter type. You have pass-by-value, and pass-by-reference, again, no different than any other type.
    Neither of these topics were specifically covered in the beginner course in "C" that I completed via a local university.
    That is because C++ is not 'C'. The two are separate languages, and knowing C does not make you experienced in C++ or have the "inside track" to learning C++.
    If anyone has a few moments and no inclination toward focusing resentment at those trying to move beyond the basics, and to achieve a better grasp of certain "basics", I'd love to know what a "namespace" is
    http://www.cplusplus.com/doc/tutorial/namespaces/
    www.parashift.com
    To keep things up to date (and formatted in conformance with local standards), the code I'm using to seek to create and pass a vector containing structured data elements follows (and is contained within a "form" in the code I've written):
    The way you learn is to write smaller programs so that you become familiar with the topic.
    Code:
    #include <vector>
    #include <vector>
    #include <iostream>
    
    struct foo
    {
       int x;
       int y;
       foo(int x_=0, int y_=0) : x(x_), y(y_) {}
    };
    
    typedef std::vector<foo> FooVector;
    
    void SomeFunction(FooVector& fV)
    {
       fV.push_back(foo(10, 20));
    }
    
    using namespace std;
    
    int main()
    {
       FooVector v;
       SomeFunction( v );
       cout << v[0].x << ", " << v[0].y;
    }
    These are the types of small programs you should be writing so that you become familiar with the topic.

    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Feb 2005
    Posts
    34

    Re: Trying to create a vector using a structure definition as the basis in VC++

    Thank you, 2kaud. I believe that if you'll check the content that is relevant to what I'm asking, there is no specific reference to the "managed code" generated by VC++. I'm seeking to debug a program strictly relative to passing a vector of structured data using a pointer. (How can that be "managed code" specific, or even relevant?)

    Based on your prior response. I presume that one could have two namespaces, without necessarily confusing the compiler, unless there are duplicate definitions in each namespace?

    Thanks!

  13. #13
    Join Date
    Feb 2005
    Posts
    34

    Re: Trying to create a vector using a structure definition as the basis in VC++

    Dear Mr. McKenzie:

    Thank you for that bit of code. I'll be reviewing and enjoying it this evening.

  14. #14
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,746

    Re: Trying to create a vector using a structure definition as the basis in VC++

    This compiles under VS taken from your code.

    Code:
    #include <vector>
    using namespace std;
    
    struct SizeAll{
            double year;
            double R;
            SizeAll() { year = 0; R = 0; }
            };
    
    int write_to_file(int count, const vector<SizeAll>& data, char const *fileName)
    {
    FILE*	fp;
    
    	fp = fopen(fileName, "w");
    	if (fp == NULL) return -1;
    	while (count-- > 0) {
    	    fprintf(fp, "%d,%d\n", data[0].year, data[0].R);
    		//check for errors writing to file
    		if (ferror(fp)) return -1;
    	}
    	fclose(fp);
    	return 0;
    }
    
    int main()
    {
    int     counter,
    	retval,
    	count=0;
    
    char const NameFile[]="D:\\Friedout.csv";
    
    vector<SizeAll>	Universe;
    vector<SizeAll> *heretis;
    
    	Universe.resize(5);
    	heretis=&Universe;
    
    	Universe[0].R = double(366);
            Universe[0].year = double(188);
    
    	for ( counter = 15000 ; counter > 99 ; counter-- ) {
                if (counter==100||counter ==2500||counter==5000||counter==7500||counter==10000||counter==12500||counter==15000) {
    			Universe[count].year =double(counter);
    			Universe[count].R = double(counter*2);
    			count = count + 1;
    			retval=write_to_file(1, Universe, NameFile);
    			if (retval == -1)  {
    				break;
    			}
    		}
    	}
    	return 0;
    }
    Why are you using heretis as a pointer to vector<SizeAll> then setting address to Universe?

    Incidentially, instead of using vector<SizeAll> everywhere, you can define a typedef for this as

    Code:
    typedef vector<SizeAll> SizeVec;
    Then whereever you use vector<SizeAll> you can then just use SizeVec

    Code:
    int write_to_file(int count, const SizeVec& data, char const *fileName)
    ...
    
    SizeVec Universe;
    SizeVec *heretis;

  15. #15
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,746

    Re: Trying to create a vector using a structure definition as the basis in VC++

    The whole point of having namespaces is so that you don't have name clashes. c++ comes with a string class defined in the std namespace. If I want my own class called string I can create a new namespace called say myspace and define my implementation of string within this namespace. As the namespaces are different, there is no conflict of names which there would be if namespaces were not used.

    Code:
    std::string stdstring;
    myspace::string mystring;
    This problem of conflicting names is particularly problematic if you obtain libraries from 3rd parties.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center