Passing Vector as a Reference

This is a discussion on Passing Vector as a Reference within the C++ Programming forums, part of the General Programming Boards category; I am having a problem passing a vector as a reference parameter and can not figure out why the program ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Location
    Gainesville
    Posts
    12

    Passing Vector as a Reference

    I am having a problem passing a vector as a reference parameter and can not figure out why the program is not compiling or working. It is meant to read in text from a file, manipulate the vector into a new one, then print out the new one to a file. Everything is working except for the method that manipulates the vector. The syntax seems correct from reading several websites which is what confuses me. I have a header file but there is nothing wrong with it. Any hints are greatly appreciated!

    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include "hw1.h"
    
    using namespace std;
    
    int main() {
    	int x;
    	vector<int> first;
    	vector<int> second;
    	ifstream inFile;
    	inFile.open("input.txt");
    		while (inFile >> x) {
    			first.push_back(x);
    		}
    		inFile.close();
    		method(first, second);
    		
    	ofstream outFile;
    	outFile.open("output.txt");
    	for (int i = 0; i < second.size(); i++) {
    		outFile << second[i] << endl;
    	}
    		outFile.close();
    		return 0;
    		
    }
    
    void method(vector<int>& x, vector<int>& y) {
    	int step = 0;
    	int j = 1;
    	vector<int> temp(x.size() / 2);
    	for (int i = 0; i < x.size() / 2; i++) {
    		y[i] = x[step] + x[j];
    		j+= 2;
    		step+= 2;
    	}
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What doesn't work about it (assuming hw1.h contains a legitimate prototype of the function)?

  3. #3
    Registered User
    Join Date
    Jan 2011
    Location
    Gainesville
    Posts
    12
    hw1.h:8: error: variable or field ‚addEven2OddLines‚ declared void
    hw1.h:8: error: ‚vector‚ was not declared in this scope
    hw1.h:8: error: expected primary-expression before ‚int‚
    hw1.h:8: error: ‚vector‚ was not declared in this scope
    hw1.h:8: error: expected primary-expression before ‚int‚
    hw1prob2.cpp: In function ‚int main()‚:
    hw1prob2.cpp:15: error: request for member ‚push_back‚ in ‚first‚, which is of non-class type ‚std::vector<int, std::allocator<int> >()‚
    hw1prob2.cpp:18: error: ‚addEven2OddLines‚ was not declared in this scope
    hw1prob2.cpp:22: error: request for member ‚size‚ in ‚second‚, which is of non-class type ‚std::vector<int, std::allocator<int> >()‚
    hw1prob2.cpp:23: warning: pointer to a function used in arithmetic


    the header file is:

    Code:
    #ifndef HW1_H
    #define HW1_H
    
    template <class T>
    void make2dArray(T ** &x, int numberOfRows, int rowsize[]);
    template <class T>
    void freeGen2dArray(T ** &x, int numberOfRows);
    void method(vector<int>& x, vector<int>& y);
    
    #endif
    the method signatures match so they should not cause a compiling error.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    No such thing as vector. There is such a thing as std::vector, though.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Your vector 'y', otherwise known in main as 'second' is of zero length, so writing to y[i] is a buffer overrun.

    Are you sure it's 'temp' you want to resize to x.size()/2 and not 'y'? Afterall, you aren't using 'temp' anywhere. Or perhaps you meant to write to 'temp' and swap 'temp' and 'y' afterwards.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,432
    The reason for the compilaton errors is that #include "hw1.h" precedes the "using namespace std;". So, when compiling the contents of hw1.h, the compiler cannot find the vector class.

    It is generally VERY bad practice have a header file rely on a "using namespace" directive. So change the references to vector<int> in hw1.h to std::vector<int>.

    It would also be advisable that hw1.h include <vector> before the function declarations. That makes it self-contained.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Location
    Gainesville
    Posts
    12

    Thumbs up

    Thank my for the advice about including <vector> in the header class regarding the method signature. I changed that one simple line and the program compiled perfectly and executed flawlessly. C++ is so different in many aspects from Java but so similar in others.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by gbman88 View Post
    C++ is so different in many aspects from Java but so similar in others.
    IMHO if you're learning C++, the less you know about Java, the better!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,432
    Quote Originally Posted by iMalc View Post
    IMHO if you're learning C++, the less you know about Java, the better!
    The reverse is also true, unless you're a C++ programmer who is a glutton for punishment
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Passing Argument from incompatible pointer type
    By AmritxD in forum C Programming
    Replies: 3
    Last Post: 08-15-2010, 03:23 PM
  3. Problem Passing Structure Reference To Function
    By soj0mq3 in forum C Programming
    Replies: 9
    Last Post: 04-24-2010, 10:27 AM
  4. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  5. Question about OpenGL/Linux
    By Ideswa in forum Linux Programming
    Replies: 12
    Last Post: 09-10-2006, 05:56 AM

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