473,382 Members | 1,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Couldn't understand what my error means

31
I got this error:

error C2664: 'Vector::setVector' : cannot convert parameter 1 from 'const std::ifstream' to 'std::ifstream'

How can it not be able to convert from std::ifstream to std::ifstream???

Here's the relevant code (I didn't put all the variables as those are not relevant):

header file:
class Matrix{

public:
.
.
.
.

void setMatrix(ifstream txtFile);

};

void Matrix::setMatrix (ifstream txtFile) {
int x;

for (int i = 0; i < this->rowLength; i++) {
for (int j = 0; j < this->columnLength; j++) {
txtFile >> x;
this->contents[i][j] = x;
}
}
}

cpp file:

void main() {
string txtFileName;

Matrix matrix1;

cout << "Enter name of vector/matrix file (use a txt file): ";
cin >> txtFileName;

ifstream txtFile(txtFileName.c_str());


matrix1.setMatrix (txtFile); //error occurs here
}
Sep 18 '07 #1
21 2518
blackx
31
I posted the wrong error code, here's the relevant error code:

error C2664: 'Matrix::setMatrix' : cannot convert parameter 1 from 'std::ifstream' to 'std::ifstream'
No copy constructor available for class 'std::basic_ifstream<_Elem,_Traits>' or constructor attempts to perform illegal conversion to non-__gc reference
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
Sep 18 '07 #2
Savage
1,764 Expert 1GB
Have you tryed passing it with reference:

void Matrix::setMatrix(const ifstream &txtFile);

??
PS:Please use code tags(#button)
Savage
Sep 18 '07 #3
blackx
31
thanks for replying,

if I pass by reference,

in main();

should i use:

matrix1.setMatrix (txtFile); //there's no compile error here but the values returned are memory addresses, not the actual values

or

matrix1.setMatrix (&txtFile); //this gives a compile error

error C2664: 'Matrix::setMatrix' : cannot convert parameter 1 from 'std::ifstream *__w64 ' to 'std::ifstream &'
Sep 18 '07 #4
Savage
1,764 Expert 1GB
thanks for replying,

if I pass by reference,

in main();

should i use:

matrix1.setMatrix (txtFile); //there's no compile error here but the values returned are memory addresses, not the actual values

or

matrix1.setMatrix (&txtFile); //this gives a compile error

error C2664: 'Matrix::setMatrix' : cannot convert parameter 1 from 'std::ifstream *__w64 ' to 'std::ifstream &'
There is no difference in function call when your function takes a pass by reference and pass by value.The problem is somewhere else.

How did you found that return values are memory addresses?

Savage
Sep 18 '07 #5
blackx
31
using

matrix1.setMatrix (txtFile);

I run the code and got like -85988360 as values in my matrix (must be memory addresses)

any suggestions on how to fix my code?
Sep 18 '07 #6
Savage
1,764 Expert 1GB
using

matrix1.setMatrix (txtFile);

I run the code and got like -85988360 as values in my matrix (must be memory addresses)

any suggestions on how to fix my code?
Try couting x from inside the setMatrix() function.

Savage
Sep 18 '07 #7
blackx
31
I actually have a different function, printMatrix() that does the cout. But I will try to include the cout inside the setMatrix function and see what I get.
Sep 18 '07 #8
Savage
1,764 Expert 1GB
I actually have a different function, printMatrix() that does the cout. But I will try to include the cout inside the setMatrix function and see what I get.
It's only for debug purposes.

Savage
Sep 18 '07 #9
blackx
31
okay something is definitely wrong with my code, I tried 2 different methods of reading.

the txt file reads:

1.0 2.0 3.6 4.0

1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

but when I run the code (with the cout inside the setVector and setMatrix functions), I got
Vector:
1.0000 1.0000 1.0000 1.0000

Matrix:
1.0000 1.0000 1.0000 1.0000
1.0000 1.0000 1.0000 1.0000
1.0000 1.0000 1.0000 1.0000
1.0000 1.0000 1.0000 1.0000

Here's the code for the function:

method 1
Expand|Select|Wrap|Line Numbers
  1. void Vector::setVector (string txtFileName) {
  2.     int x;
  3.  
  4.     ifstream txtFile(txtFileName.c_str());
  5.  
  6.     for (int i = 0; i < this->length; i++) {
  7.         txtFile >> x;
  8.         this->contents[i] = x;            
  9.     }
  10.  
  11.     int numberWidth = 8;
  12.     int decimalPlaces = 4;
  13.  
  14.     for (int i = 0; i < this->length; i++) {
  15.         cout.width(numberWidth); 
  16.         cout << fixed << setprecision(decimalPlaces) << this->contents[i];
  17.         cout << '\t';
  18.     }
  19.  
  20.     cout << endl;
method 2:
Expand|Select|Wrap|Line Numbers
  1. void Matrix::setMatrix (ifstream &txtFile) {
  2.     int x;
  3.  
  4.     for (int i = 0; i < this->rowLength; i++) {
  5.         for (int j = 0; j < this->columnLength; j++) {
  6.             txtFile >> x;
  7.             this->contents[i][j] = x;            
  8.         }
  9.     }
  10.  
  11.     int numberWidth = 8;
  12.     int decimalPlaces = 4;
  13.  
  14.     for (int i = 0; i < this->rowLength; i++) {
  15.         for (int j = 0; j < this->columnLength;  j++) {
  16.             cout.width(numberWidth); 
  17.             cout << fixed << setprecision(decimalPlaces) << this->contents[i][j];
  18.             cout << '\t';
  19.         }
  20.         cout << endl; 
  21.     }
  22.  
  23.     cout << endl;
  24. }
  25.  
Sep 18 '07 #10
blackx
31
for the main, I have:

Expand|Select|Wrap|Line Numbers
  1. cout << "Enter name of vector/matrix file (testMatrix.txt): ";
  2.     cin >> txtFileName;
  3.  
  4.     ifstream txtFile(txtFileName.c_str());
  5.  
  6.     vector1.setVector (txtFileName); 
  7.     matrix1.setMatrix (txtFile);
  8.  
Sep 18 '07 #11
Savage
1,764 Expert 1GB
for the main, I have:

Expand|Select|Wrap|Line Numbers
  1. cout << "Enter name of vector/matrix file (testMatrix.txt): ";
  2.     cin >> txtFileName;
  3.  
  4.     ifstream txtFile(txtFileName.c_str());
  5.  
  6.     vector1.setVector (txtFileName); 
  7.     matrix1.setMatrix (txtFile);
  8.  

Perform a check to see if file is open.

Savage
Sep 18 '07 #12
blackx
31
Perform a check to see if file is open.

Savage

what do you mean by checking if a file is open. I'm opening the file with the setMatrix function.

Thanks.
Sep 18 '07 #13
Savage
1,764 Expert 1GB
what do you mean by checking if a file is open. I'm opening the file with the setMatrix function.

Thanks.

ifstream,has a method called IsOpen(),which checks if file has been opened and returns a bool.Perhaps your file has not been opened and you have initialized in constructor of both vector and matrix values to 1.0

Every time you open a file you should perform a chck to see if file has been opened

Savage
Sep 18 '07 #14
blackx
31
I just wrote

txtFile.is_open()

in my main...how do I make it return true or false?

I initialized my values to 0, not 1, so I don't think its because of that.
Sep 18 '07 #15
Savage
1,764 Expert 1GB
I just wrote

txtFile.is_open()

in my main...how do I make it return true or false?

I initialized my values to 0, not 1, so I don't think its because of that.

Just put it into if statement:

if(txtFile.is_open())

And if it isn't that try changing in your text file first value(1.0) to something else,and see if your program than prints that.

Savage
Sep 18 '07 #16
blackx
31
I changed the first number in the txt file to 2, and yes, now I get all 2s when I run the code. Any ideas what I did wrong?
Sep 18 '07 #17
Savage
1,764 Expert 1GB
I changed the first number in the txt file to 2, and yes, now I get all 2s when I run the code. Any ideas what I did wrong?

Wait a secound,why is x a integer when you are reading in floats?

Expand|Select|Wrap|Line Numbers
  1. void Vector::setVector (string txtFileName) {
  2.           int x;
  3.  
  4.           ifstream txtFile(txtFileName.c_str());
  5.  
  6.           for (int i = 0; i < this->length; i++) {
  7.               txtFile >> x;
  8.               this->contents[i] = x;     
  9.           }
  10.  
  11.           int numberWidth = 8;
  12.           int decimalPlaces = 4;
  13.           for (int i = 0; i < this->length; i++) {
  14.               cout.width(numberWidth);
  15.               cout << fixed << setprecision(decimalPlaces) << this->contents[i];
  16.               cout << '\t';
  17.           }
  18.  
  19.           cout << endl;.
  20.       }
Sep 18 '07 #18
blackx
31
whoa! you're right!

I just changed x to double and I got what I wanted!

I just spent the last 3 hours trying to fix my code, and all I did wrong was I made x int instead of double! that's crazy.

Thanks for your help man, I really appreciate it.
Sep 18 '07 #19
Savage
1,764 Expert 1GB
whoa! you're right!

I just changed x to double and I got what I wanted!

I just spent the last 3 hours trying to fix my code, and all I did wrong was I made x int instead of double! that's crazy.

Thanks for your help man, I really appreciate it.
I'm more than happy to help you.

I think that when x was int stream became corrupted and started to continuously throw in same value.

Savage
Sep 18 '07 #20
blackx
31
I'm more than happy to help you.

I think that when x was int stream became corrupted and started to continuously throw in same value.

Savage
that makes sense. Thanks again.
Sep 18 '07 #21
Savage
1,764 Expert 1GB
that makes sense. Thanks again.
Well,that's it then.

If you face another problem with these two classes,feel free to post again.

Savage
Sep 18 '07 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: william | last post by:
Hi, I could run hello.htm, but hello.asp. They are same except the file name. ..... <body> hello. </body> ..... I set Execute Permission to Script or Excutable(windows 2000, IIS5.0) from Home...
0
by: QWERTY | last post by:
--------------Boundary-00=_O5I3QL80000000000000 Content-Type: Multipart/Alternative; boundary="------------Boundary-00=_O5I3LVC0000000000000" --------------Boundary-00=_O5I3LVC0000000000000...
10
by: forgotten field | last post by:
Hi,how are you? I have been studying C++ by myself, but recently I am having a real problem. I am learning about the basic usage of a doubly linked list using polymorphism. However, I have got the...
18
by: __frank__ | last post by:
The following code use a macro and a label. I would to change it and use instead a more readable function and avoid the label. The macro DAQmxFailed checks for the return code of the various...
2
by: Murthy | last post by:
Hi, After genarating the exe when i run the exe the following error is comming. Common Language Runtime Debugging Services Application has generated an exception that could not be handled....
1
by: ganesah | last post by:
recently i'd downloaded a source code in c++ and tried to compile and run in VS C++ 6.0. however the source code couldn't be compiled. the error is as follows> before that, there is a header file...
8
by: Joshua Moore | last post by:
/* Hi, I was hoping someone could help me with this problem. I did my work and worked my way through the usual compiler messages, but I have run against some problem I can't identify. The compiler...
0
by: Rajgodfather | last post by:
I am getting the end couldn't error while validating xml file against xsd. The xml file looks perfect. XML: <event id="1"> <!-- Successful event. --> ...
0
by: akshaycjoshi | last post by:
I am reading a book which says Even though unboxed value types don't have a type object pointer, you can still call virtual methods (such as Equals, GetHashCode, or ToString) inherited or...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.