Not declared in this scope Error, Templates

This is a discussion on Not declared in this scope Error, Templates within the C++ Programming forums, part of the General Programming Boards category; Hey again! I am getting an error with which I am unfamiliar with. It says hw1prob1.cpp: In function ‚int main()‚: ...

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

    Not declared in this scope Error, Templates

    Hey again! I am getting an error with which I am unfamiliar with. It says


    hw1prob1.cpp: In function ‚int main()‚:
    hw1prob1.cpp:20: error: expected primary-expression before ‚]‚ token
    hw1prob1.cpp:20: error: ‚makeGen2dArray‚ was not declared in this scope
    hw1prob1.cpp:32: error: no match for ‚operator<<‚ in ‚outFile << *((*(x + ((long unsigned int)(((long unsigned int)third) * 8ul)))) + ((long unsigned int)(((long unsigned int)fourth) * 4ul)))‚
    hw1prob1.cpp:34: error: no match for ‚operator<<‚ in ‚outFile << std::endl‚

    In addition, it says no match for the operator << which does not make sense because it is being used by a ifstream.

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    #include "hw1.h"
    
    int main() 
    {
    	int** x;
    	int value;
    	int numRows;
    	ifstream inFile;
    	inFile.open("matInp.txt");
    	numRows = inFile.get();
    	int rowSize [numRows];
    	for (int i = 0; i < numRows; i++) {
    		inFile >> value;
    		rowSize[i] = value;
    	}
    	makeGen2dArray(x, numRows, rowSize[]);
    	for (int start = 0; start < numRows; start++) {
    		for (int second = 0; second < rowSize[numRows]; second++) {
    			inFile >> value;
    			x[start][second] = value + 5;
    		}
    	}
    	inFile.close();
    	ifstream outFile;
    	outFile.open("matOut.txt");
    	for (int third = 0; third < numRows; third++) {
    		for (int fourth = 0; fourth < rowSize[numRows]; fourth++) {
    			outFile << x [third][fourth]*;
    		}
    			outFile << endl;
    	}
    	freeGen2dArray(x, numRows);
    	return 0;
    }
    
    template <class T>
    void makeGen2dArray(T ** &x, int numberOfRows, int rowSize[]) {
    	x = new T * [numberOfRows];
    	for (int i = 0; i < numberOfRows; i++) {
    		x[i] = new T [rowSize[i]];
    	}
    }
    
    template <class T>
    void freeGen2dArray(T ** &x, int numberOfRows) {
    	for (int i =0; i < numberOfRows; i++) {
    		delete [] x[i];
    
    		delete [] x;
    		x = NULL;
    	}
    }
    The error occurs in the line that is green. I think it may be a problem with the way I am inputting the parameter but Im not sure. Ive tried different parameter values but nothing seems to work. Any links/tips would be helpful.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Move your functions and templates *above* main...

  3. #3
    Registered User
    Join Date
    Jan 2011
    Location
    Gainesville
    Posts
    12
    I have a header file so that should not cause the problem. I just did not put the code here

    here it is:

    Code:
    #ifndef HW1_H
    #define HW1_H
    #include <vector>
    
    template <class T>
    void make2dArray(T ** &x, int numberOfRows, int rowsize[]);
    template <class T>
    void freeGen2dArray(T ** &x, int numberOfRows);
    void addEven2OddLines(std::vector<int>& x, std::vector<int>& y);
    
    #endif

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That header also has a conspicuous absence of "makeGen2dArray".

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,713
    For a template class the definition and implementation must be in the same file.

    Jim

  6. #6
    Registered User
    Join Date
    Jan 2011
    Location
    Gainesville
    Posts
    12
    Ok so template functions must be declared and implemented in the same file. The other error message still persists however regarding primary expression before a ] token. In addition it is saying that << is not a valid operator in my outFile which is a ifstream object. I checked the syntax and did not find any commas or semicolons out of place which is what other websites said was the issue with that error.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,713
    Is this line 34?

    Code:
     outFile << x [third][fourth]*;
    If it is what is the * for?

    Jim

  8. #8
    Registered User
    Join Date
    Jan 2011
    Location
    Gainesville
    Posts
    12
    It was intended to dereference the value held in that position of the array but I did not realize the formatting I had put it in already does that. The concept of pointers and a double array is still new to me. I have taken out the * dereference in my code already but I am still getting an error involving no match for a << operator. I have changed the code but the error still persists

    Code:
    for (int third = 0; third < numRows; third++) {
    		for (int fourth = 0; fourth < rowSize[numRows]; fourth++) {
    			outFile << x[third][fourth]
    					<< " ";
    		}
    			outFile << "/n";
    	}


    hw1prob1.cpp:20: error: expected primary-expression before ‚]‚ token
    hw1prob1.cpp:32: error: no match for ‚operator<<‚ in ‚outFile << *((*(x + ((long unsigned int)(((long unsigned int)third) * 8ul)))) + ((long unsigned int)(((long unsigned int)fourth) * 4ul)))‚
    hw1prob1.cpp:35: error: no match for ‚operator<<‚ in ‚outFile << "/n"‚

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    ifstream outFile;
    Just because you called it "outFile" doesn't mean it's the right type.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    2,713
    Where are you assigning memory for x?

    Also if the following line hasn't changed what is it doing?

    Code:
    numRows = inFile.get();
    Jim

  11. #11
    Registered User
    Join Date
    Jan 2011
    Location
    Gainesville
    Posts
    12
    Quote Originally Posted by tabstop View Post
    Code:
    ifstream outFile;
    Just because you called it "outFile" doesn't mean it's the right type.

    Wow you guys are catching my typos left and right. I have spent too many hours straight on this project! I have one last questions and I will leave you all alone if you have the patience. I have been searching around for hours for solutions to the error:

    error: expected primary-expression.

    In my case the line is:

    Code:
    	makeGen2dArray(x, numRows, rowSize[]);
    
    where x is declared as int **x;
    and the header file that contains the method is

    Code:
    template <class T>
    void makeGen2dArray(T ** &x, int numberOfRows, int rowSize[]) {
    	x = new T * [numberOfRows];
    	for (int i = 0; i < numberOfRows; i++) {
    		x[i] = new T [rowSize[i]];
    	}
    }
    The solutions I have found on involve people typing in types into the function rather than the passing values, constructor misuse, and wrong parameters being passed into the function. In my case, I pass in a reference to a double array known as **x which should be correct, an integer I read from a file, and an array of integers which is also correct.

    The specific error says:
    hw1prob1.cpp:20: error: expected primary-expression before ‚]‚ token


    and line 20 is:

    Code:
    	makeGen2dArray(x, numRows, rowSize[]);

  12. #12
    Registered User
    Join Date
    Jan 2011
    Location
    Gainesville
    Posts
    12
    Quote Originally Posted by jimblumberg View Post
    Where are you assigning memory for x?

    Also if the following line hasn't changed what is it doing?

    Code:
    numRows = inFile.get();
    Jim
    The infile object is reading in an integer from the specified file and storing it in the integer named numRows. I looked at the c++ api (not sure what to call in c++) and the method .get() said it read in a char value and stored it as an integer. The first value in the file is a simple integer such as 5 so the get() method should be able to read it into the file.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    rowSize[]
    You want to pass the array. Then do so. The array is called "rowSize".

  14. #14
    Registered User
    Join Date
    Jan 2011
    Location
    Gainesville
    Posts
    12
    Just made me look like a fool. Thanks. Now time to debug. Thanks for the helpful advice everyone. If you dont mind me asking , is there a good debugger for windows. Currently I have to use putty to remote logon to the unix server and it is a pain to debug this way. Besides putting a virutal linux or linux partition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: was not declared in this scope compilation error
    By i_r_tomash in forum C Programming
    Replies: 2
    Last Post: 05-27-2010, 07:44 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 08:45 PM

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