Help with accessing vector variables

This is a discussion on Help with accessing vector variables within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm just learning how to use vectors, read the FAQ on Vector and Deque and some thigns are still ...

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Help with accessing vector variables

    Hi,
    I'm just learning how to use vectors, read the FAQ on Vector and Deque and some thigns are still blurry. I've got a simple code to put some values into a vector and then read them back. I get a compiler warning when this code runs.
    Code:
    #include <vector>
    #include <string>
    #include <iostream.h>
    
    using namespace std;
    
    int main()
    {
    	string C;
    	int A;
    	int i;
    
    	vector<int> Nums_Vec;
    	vector<string> Words_Vec;
    
    	for (i=0; i<6; i++)
    	{
    		A=i;
    		Nums_Vec.push_back(A);
    
    		C="one";
    		Words_Vec.push_back(C);
    	}
    
    	for (i=0; i<6; i++)
    	{
    		printf("%d\t%s\n", Nums_Vec[i], Words_Vec[i].c_str());
    	}
    
    	return 0;
    	
    }

    And also, can I put a vector inside a class, something like this:

    Code:
    class FSAD        //Fixed/Selectable Analog Data  
    	{
    	public:
    
    		char Name[25];
    		int nExpected;  // size = nExected*2+1
    
    		vector<DataLine> DL_Vec;		
    	};
    Right now it doesn't recognise the syntax. Error I get:
    Code:
    h:\code\edfparse3\edfparse3.h(34) : error C2143: syntax error : missing ';' before '<'
    h:\code\edfparse3\edfparse3.h(34) : error C2501: 'vector' : missing storage-class or type specifiers
    h:\code\edfparse3\edfparse3.h(34) : error C2059: syntax error : '<'
    h:\code\edfparse3\edfparse3.h(34) : error C2238: unexpected token(s) preceding ';' ........
    Thanks.
    Last edited by earth_angel; 06-28-2005 at 07:56 AM.

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    This works fine for me:

    Code:
    #include <vector>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main() {
        string C;
        int A, i;
        vector<int> Nums_Vec;
        vector<string> Words_Vec;
    
        for (i=0; i<6; i++) {
            A=i;
            Nums_Vec.push_back(A);
    
            C="one";
            Words_Vec.push_back(C);
        }
    
        for (i=0; i<6; i++) {
            cout << Nums_Vec[i] << "\t" << Words_Vec[i] << endl;
        }
        return 0;
    }
    You should not be mixing C and C++ IO.

    I believe I already gave you an example for embedding a vector inside a class, one even specific to your current problem.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Yep,your code compiles and runs, so does mine, but before it was giving a warning. It's wierd, it misbehaves sometimes.

    and I based this class thing on your example, but for some reason it doesn't recognise the vector syntax. I get these error:
    Code:
    h:\code\edfparse3\edfparse3.h(33) : error C2143: syntax error : missing ';' before '<'
    h:\code\edfparse3\edfparse3.h(33) : error C2501: 'vector' : missing storage-class or type specifiers
    h:\code\edfparse3\edfparse3.h(33) : error C2059: syntax error : '<'   ................. etc
    This is from this defenition:
    Code:
    class FSAD        //Fixed/Selectable Analog Data  
    	{
    	public:
    
    		char Name[25];
    		int nExpected;  // size = nExected*2+1
    		vector < DataLine > vctDL;		
    	};
    and I don't even notice when I mix C and C++, I'll pay more attention to it.
    Last edited by earth_angel; 06-28-2005 at 08:36 AM.

  4. #4
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    You MUST declare the structure DataLine BEFORE you use it. My example in the other thread compiles and runs on my machine, have you tried it yet?

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >h:\code\edfparse3\edfparse3.h(33) : error C2143: syntax error : missing ';' before '<'
    Did you include <vector> within edfparse3.h?

  6. #6
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I did include <vector>.

    You MUST declare the structure DataLine BEFORE you use it
    the struct and the class are defined in the header file. 1st the struct then the class, so it know what DataLine is. If I declare the struct and the class in the main .cpp file without a header file, it runs, but doesn't let me access the members of the struct, Unless I'm doing it wrong:
    Code:
    #include<cstdio>
    #include <vector>
    #include <iostream>
    
    using namespace std;
    
    struct DataLine{//everything public by default
    	char Param[20];
    	int Label, SFactor, SigBits;
    	int Bit30, Bit31, Max, Min;
    	DataLine(){//a REALLY GOOD IDEA to have a constructor initialize variables
    		Label=SFactor=SigBits=Bit30=Bit31=Max=Min=0;
    		memset(Param, 0, sizeof(Param));
    	};
    } ;
    
    class FSAD{        //Fixed/Selectable Analog Data  
    public:
    	char Name[25];
    	int nExpected;  // size = nExected*2+1
    	vector < DataLine > vectDL;
    
    	//void MakeSec() no need for this now
    };
    
    void ReadDataSec(FILE * inp, FSAD &Section , int nExpected)//note that Section MUST be passed in by reference!
    {
    	char line[100];
    	int  i=0;
    	DataLine dl;
    
    	while ( fgets(line, sizeof line, inp) != NULL ) // reads in a line of text from file
    	{
    		//if ( strncmp(line, "ENDL", 4) == 0 ) // Exit if at the end of a section
    		//{
    			sscanf(line, "%s",dl.Param);
    			printf("%s \n", dl.Param);
    			Section.vectDL.push_back(dl);//this does a copy, this could be more efficient with references, but I wouldn't worry about that now.
    
                break;
    	}
             //}
    
    }
    
    int main()
    {
    	FSAD test;
    	FILE *inp;
    	inp = fopen("J:\\EICAS Simulator\\Engine Files\\615F.eng", "r");
    
    	int n = 16;
    
    	ReadDataSec(inp, test, n);
    
    	cout<<endl<<test.vectDL.Param<<endl;
    
    	return 0;
    }
    If I use a header file and define the function in its own .cpp file, I get the errors I mentioned before.

    Why is it so confused?
    Last edited by earth_angel; 06-28-2005 at 09:11 AM.

  7. #7
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Post all your code. I believe you can zip it up and attatch it after renaming it to '.txt'.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  8. #8
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I adjusted last post. and here are the functions that give me trouble in my actual project.

    And I can't spell apperantly.....

    And I had to spesify a location in the vector and then the member of the struct. I got that now.
    Code:
    cout<<endl<<test.vectDL[0].Param<<endl;
    In the project with the header file, it still doesn't like the < > operators. I adjusted the call to the function:
    Code:
    ReadDataSec(inp, FAD, FAD.nExpected);
    Last edited by earth_angel; 06-28-2005 at 09:36 AM.

  9. #9
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    OK, the most basic problem is in your header, you need to declare the vector as 'std::vector' (it is a 'bad thing' to use 'using namespace ...' in any header). The second problem is when you call ReadDataSec, your prototype has Section passed as reference (remember my comment), but your call is passing it as a pointer. Just drop the '&' in the call and everything will be fine. The third, and rather minor issue, is the vector variable is named vctDL in the class definition, but it is used as vectDL in ReadDataSec(). The first two may have you a bit at wits end with a lack of experience (you get used to seeing these errors with experience and then fix them by reflex (hence my comment in my sig)), but I am sure the third one you would have caught if you were not bamboozled by the others.

    One of the main problems with posting code bits is that we all make assumptions about what is missing from the post. For future reference, you can reduce the churning by posting complete working examples of your problematic code.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  10. #10
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    OK, this is stupid, but I didn't write a line in my header and it didn't work. Guess what it is...






    Code:
    using namespace std;




    Yeeeeeh, now it works........ Sorry for the trouble.

  11. #11
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    If I don't use the using line, and specify std::vector just in the header file when I declare the class, will it cause any problems down the road/ Will I have to use std:: everytime I access the vector? and why (not)?

    The rest of the errors I caught. I just made so many changes that I must've lost track of what I was trying to do. But now it seems to work. Onto the next part of the program...
    and I'll probably be back.....
    Thanks you very much..... You need a Thank You card.....

  12. #12
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    If you put the namespace declaration in your header, then you can have problems with names (which is why namespaces were invented, doanchano). It is pretty safe to put it at the top of any source files so you can leave off the 'std::', but it ain't no big thing to put it in there. You can also put a 'using namespace...' inside any block of code, so you can use it inside a function/method body, inside a for/while loop, etc.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  13. #13
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    OK, that's clear now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector : accessing values
    By rahulsk1947 in forum C++ Programming
    Replies: 1
    Last Post: 06-01-2009, 09:26 PM
  2. accessing a variable from another method
    By larne in forum C++ Programming
    Replies: 14
    Last Post: 01-16-2009, 03:24 PM
  3. Problems accessing logged on user via System.Security
    By conor20_ie in forum C# Programming
    Replies: 5
    Last Post: 11-16-2007, 06:52 AM
  4. accessing a struct with ->
    By Anddos in forum C++ Programming
    Replies: 1
    Last Post: 04-23-2006, 03:28 PM
  5. accessing documents in a MDI app
    By Tesita in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2001, 05:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21