Printing a dat file to the screen using a matrix

Started by
3 comments, last by Derakon 14 years, 9 months ago
So I have to print a dat file to the screen using a matrix. Here is the code I have so far and I consistently get 25 errors. I have tried putting the apmatrix.h file int he include folder and adding the apmatrix.cpp files to the project and nothing works. When I wrote this program in the classroom I got it to compile but nothing printed to the screen. Any help #include <iostream.h> #include "apmatrix.h" #include <fstream.h> int main() { apmatrix <char> secret(6,6); ifstream infile; ifstream infile_alpha; infile.open("alpha.dat", ios::in); cout<< secret; system("PAUSE"); return 0; } ERRORS ______________________ Compiler: Default compiler Building Makefile: "C:\Dev-Cpp\Makefile.win" Executing make... make.exe -f "C:\Dev-Cpp\Makefile.win" all g++.exe -D__DEBUG__ -c apmatrix.cpp -o apmatrix.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include" -g3 In file included from C:/Dev-Cpp/include/c++/3.4.2/backward/iostream.h:31, from C:/Dev-Cpp/include/apvector.cpp:16, from C:/Dev-Cpp/include/c++/3.4.2/backward/apvector.h:127, from C:/Dev-Cpp/include/c++/3.4.2/backward/apmatrix.h:4, from apmatrix.cpp:12: C:/Dev-Cpp/include/c++/3.4.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated. apmatrix.cpp:18: error: redefinition of `apmatrix<itemType>::apmatrix()' C:/Dev-Cpp/include/apmatrix.cpp:18: error: `apmatrix<itemType>::apmatrix()' previously declared here apmatrix.cpp:29: error: redefinition of `apmatrix<itemType>::apmatrix(int, int)' C:/Dev-Cpp/include/apmatrix.cpp:29: error: `apmatrix<itemType>::apmatrix(int, int)' previously declared here apmatrix.cpp:45: error: redefinition of `apmatrix<itemType>::apmatrix(int, int, const itemType&)' C:/Dev-Cpp/include/apmatrix.cpp:45: error: `apmatrix<itemType>::apmatrix(int, int, const itemType&)' previously declared here apmatrix.cpp:68: error: redefinition of `apmatrix<itemType>::apmatrix(const apmatrix<itemType>&)' C:/Dev-Cpp/include/apmatrix.cpp:68: error: `apmatrix<itemType>::apmatrix(const apmatrix<itemType>&)' previously declared here apmatrix.cpp:86: error: redefinition of `apmatrix<itemType>::~apmatrix()' C:/Dev-Cpp/include/apmatrix.cpp:86: error: `apmatrix<itemType>::~apmatrix()' previously declared here apmatrix.cpp:96: error: redefinition of `const apmatrix<itemType>& apmatrix<itemType>::operator=(const apmatrix<itemType>&)' C:/Dev-Cpp/include/apmatrix.cpp:96: error: `const apmatrix<itemType>& apmatrix<itemType>::operator=(const apmatrix<itemType>&)' previously declared here apmatrix.cpp:116: error: redefinition of `int apmatrix<itemType>::numrows() const' C:/Dev-Cpp/include/apmatrix.cpp:116: error: `int apmatrix<itemType>::numrows() const' previously declared here apmatrix.cpp:123: error: redefinition of `int apmatrix<itemType>::numcols() const' C:/Dev-Cpp/include/apmatrix.cpp:123: error: `int apmatrix<itemType>::numcols() const' previously declared here apmatrix.cpp:140: error: redefinition of `void apmatrix<itemType>::resize(int, int)' C:/Dev-Cpp/include/apmatrix.cpp:140: error: `void apmatrix<itemType>::resize(int, int)' previously declared here apmatrix.cpp:157: error: redefinition of `const apvector<itemType>& apmatrix<itemType>::operator[](int) const' C:/Dev-Cpp/include/apmatrix.cpp:157: error: `const apvector<itemType>& apmatrix<itemType>::operator[](int) const' previously declared here apmatrix.cpp:172: error: redefinition of `apvector<itemType>& apmatrix<itemType>::operator[](int)' C:/Dev-Cpp/include/apmatrix.cpp:172: error: `apvector<itemType>& apmatrix<itemType>::operator[](int)' previously declared here make.exe: *** [apmatrix.o] Error 1 Execution terminated
Advertisement
Your errors are all in apmatrix.cpp, which you haven't shown to us.

From what you said (You were able to compile in class, but not at home), it sounds like your home compiler is set up differently from the one in class, and doesn't like the apmatrix.cpp file.

The reason you didn't get any output in class is because you didn't send the data in alpha.dat to your matrix class. You just opened an input stream and then did nothing with it. You need to load the data into your matrix before you can display it.
Jetblade: an open-source 2D platforming game in the style of Metroid and Castlevania, with procedurally-generated levels
how do I load alpha.dat into my matrix?

and thanks for helping
For one thing, I would close the file after using it.

infile.close();

Also, it is safer to check and see if the file is actually open before you begin using it. What happens if you accidentally enter the wrong filename and the compiler looks to open a file that doesn't exist?

You can use if(infile.is_open()) { .... } to make sure of that.

Make sure you do everything with the file after that. For instance...

if(infile.is_open()) {

while(!infile.eof()) {
//Reading the file in here...
}

infile.close();
}

infile.eof() checks to see if you are at the end of the file. In order to read the file, you have to loop through the file until infile.eof() returns true.

Right now you are just printing to the console window (which is wrong anyways, unless you have overloaded the << operator). The file is open, but it is doing nothing in the code.

To read from the file, you use the >> operator, but with the filename instead of cin. For instance:

char c;
infile >> c;

Which will read a character from the file.
So what you have to do is read from the file into your matrix.

As for the problems you are getting, it looks like you are redefining the apmatrix class which is usually caused by using #include on the same header file more than once.

The best way to prevent that is by using an include guard on the class (which we haven't seen yet).

This is done by:

#ifndef APMATRIX_H
#define APMATRIX_H

//Class header goes here

#endif

That prevents the class from being redefined.
As for how to load the file into the apmatrix class, that depends entirely on how that class is implemented. We know nothing about that class right now except that its constructor takes two arguments, so it's rather hard for us to advise you on that. :)
Jetblade: an open-source 2D platforming game in the style of Metroid and Castlevania, with procedurally-generated levels

This topic is closed to new replies.

Advertisement