Thread: vectors and fuctions

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    44

    vectors and fuctions

    Code:
    #include<iostream>
    #include<vector>
    #include<string>
    
    using namespace std;
    
    void builtVector(string vectorName);
    //void sortVector(string vectorName);
    
    int main()
    {
    	builtVector("v");
    	//sortVector(v);
    	for(int i = 0; i <= v.size(); i++)
    	{
    		cout << v[i];	
    	}
    	return 0;
    }
    
    vector<int> builtVector(final string vectorName)
    {
    	vector<int>	vectorName;
    	vectorName.push_back(4);
    	vectorName.push_back(2);
    	vectorName.push_back(7);
    	vectorName.push_back(1);
    	
    	return vectorName;	
    }
    
    --------------------Configuration: mingw2.95 - CUI Release, Builder Type: MinGW (Old)--------------------
    
    Compiling C:\Program Files\C-Free 4\temp\Untitled3.cpp...
    [Error] C:\Program Files\C-Free 4\temp\Untitled3.cpp:14: `v' undeclared (first use this function)
    [Error] C:\Program Files\C-Free 4\temp\Untitled3.cpp:14: (Each undeclared identifier is reported only once
    [Error] C:\Program Files\C-Free 4\temp\Untitled3.cpp:14: for each function it appears in.)
    [Error] C:\Program Files\C-Free 4\temp\Untitled3.cpp:21: parse error before `vectorName'
    
    Complete Compile C:\Program Files\C-Free 4\temp\Untitled3.cpp: 4 error(s), 0 warning(s)
    well I have some questions that have to do with some basic c++, but I cannot figure it out by myself so here I am.

    I use builtVector to initialize a vector, with param string vectorName; so when I call builtVector("v"); a vector v should be initialized. Anyway I get the above errors.
    Also if I have to return a vector should the return type of my fuction be vector or vector<int>???

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    1. There is no "final" keyword in C++

    2. vectorName is a string, not a vector.

    3. What book are you using to learn C++?

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    44
    Code:
    int main()
    {
    	
    	builtVector();
    	//sortVector(v);
    	/*for(int i = 0; i < x; i++)
    	{
    		cout << v[i];	
    	}*/
    	return 0;
    }
    
    void builtVector()
    {
    	vector<int> v;
    	v.push_back(4);
    	v.push_back(2);
    	v.push_back(7);
    	v.push_back(1);
    	
    	return ;	
    }
    I modified it "a bit". The thing is I want to make a fuction with a parameter the vector's name and I have trouble making it. Can you help?

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    You either need to build the vector and return it, or pass it by reference or pointer.

    You should be learning with a book - these are very basic concepts.

    This one is free: Thinking in C++

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Are you trying to use pass by reference?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The name of the vector inside the function is irrelevant outside the function, so there is no need to pass a name like you do (naming vectors like that is not possible in C++ anyway).
    By naming, isn't this sufficient?:
    Code:
    std::vector<int> VectorName = BuildVector();
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    44
    Code:
    int main()
    {
    	//sortVector(v);
    	vector<int> x = builtVector();
    	for(int i = 0; i < x.size(); i++)
    	{
    		cout << x[i];	
    	}
    	return 0;
    }
    vector<int> builtVector()
    {
    	vector<int> v;
    	v.push_back(4);
    	v.push_back(2);
    	v.push_back(7);
    	v.push_back(1);
    	
    	return v;	
    }
    Now I have this, and I am trying the pointer solution. I am having trouble though!!!

    You ment making a pointer pointing to the vector? Can you spoon feed me some code to work with???I already have the thinking in c++, I am just playing around now without the book.
    Last edited by mixalissen; 05-05-2008 at 08:51 AM.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    I'd actually use a reference, and not a pointer. Sorry, spoon feeding is no fun - learn for yourself and you'll be much happier. If you have the book, look up references and passing function arguments by reference. You can figure it out. Post your first attempt at doing this.

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I wouldn't recommend jumping into pointers and references until you got the very basics first (like functions)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    44
    ok first attempt to built with pointers

    Code:
    void  built(vector<int>* x);
    
    int main()
    {
    	vector<int> v;
    	built(&v);
    
    	return 0;
    }
    
    void built(vector<int>* x)
    {
    	*x.push_back(3);	
    	*x.push_back(4);
    	return ;
    }
    
    Compiling C:\Documents and Settings\mixalis\Επιφάνεια εργασίας\C++ applications\passing by pointer.cpp...
    [Error] C: request for member `push_back' in `x', which is of non-aggregate type `vector<int,allocator<int> > *'
    [Error] C:\: request for member `push_back' in `x', which is of non-aggregate type `vector<int,allocator<int> > *'
    the main idea is :
    Code:
    void foo(int* n)
    {
    	*n = 0;
    }
    
    void foo(int& n)
    {
    	n = 0;
    }
    
    int main()
    {
    	int n;
    	foo(&n);
    	return 0;
    }
    right???

    give some feedback. I swear I will study from the book and not continue misusing my time, jumping from example to example on the Internet ))))

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    *x.push_back(3) is the equivalent of *(x.push_back(3)). What you want is (*x).push_back(3) or more commonly x->push_back(3).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    44
    Great that's what I was looking for.But what about the get fuction?? How would your suggestion work on:
    Code:
    void get(vector<int>* y)
    {
    	for(int i = 0; i < y->size(); i++) (1)-----------
    	{
    		cout << &y[i];
    	}
    	
    	return;
    }
    This way I get the address of the variable not the variable stored there??? Any ideas???
    I hope (1) is correct. Thanx again

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This way I get the address of the variable not the variable stored there?
    You would use: (*y)[i].

    I hope (1) is correct.
    Almost. i should be a vector<int>::size_type, not an int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    44
    Thanx for that, really helpful, it took all afternoon but at least I learned something...

Popular pages Recent additions subscribe to a feed