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
}
21 2518
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>
]
Have you tryed passing it with reference:
void Matrix::setMatrix(const ifstream &txtFile);
??
PS:Please use code tags(#button)
Savage
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 &'
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
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?
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
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.
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
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 -
void Vector::setVector (string txtFileName) {
-
int x;
-
-
ifstream txtFile(txtFileName.c_str());
-
-
for (int i = 0; i < this->length; i++) {
-
txtFile >> x;
-
this->contents[i] = x;
-
}
-
-
int numberWidth = 8;
-
int decimalPlaces = 4;
-
-
for (int i = 0; i < this->length; i++) {
-
cout.width(numberWidth);
-
cout << fixed << setprecision(decimalPlaces) << this->contents[i];
-
cout << '\t';
-
}
-
-
cout << endl;
-
}
method 2: -
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;
-
}
-
}
-
-
int numberWidth = 8;
-
int decimalPlaces = 4;
-
-
for (int i = 0; i < this->rowLength; i++) {
-
for (int j = 0; j < this->columnLength; j++) {
-
cout.width(numberWidth);
-
cout << fixed << setprecision(decimalPlaces) << this->contents[i][j];
-
cout << '\t';
-
}
-
cout << endl;
-
}
-
-
cout << endl;
-
}
-
for the main, I have: -
cout << "Enter name of vector/matrix file (testMatrix.txt): ";
-
cin >> txtFileName;
-
-
ifstream txtFile(txtFileName.c_str());
-
-
vector1.setVector (txtFileName);
-
matrix1.setMatrix (txtFile);
-
for the main, I have: -
cout << "Enter name of vector/matrix file (testMatrix.txt): ";
-
cin >> txtFileName;
-
-
ifstream txtFile(txtFileName.c_str());
-
-
vector1.setVector (txtFileName);
-
matrix1.setMatrix (txtFile);
-
Perform a check to see if file is open.
Savage
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.
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
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.
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
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?
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? - void Vector::setVector (string txtFileName) {
-
int x;
-
-
ifstream txtFile(txtFileName.c_str());
-
-
for (int i = 0; i < this->length; i++) {
-
txtFile >> x;
-
this->contents[i] = x;
-
}
-
-
int numberWidth = 8;
-
int decimalPlaces = 4;
-
for (int i = 0; i < this->length; i++) {
-
cout.width(numberWidth);
-
cout << fixed << setprecision(decimalPlaces) << this->contents[i];
-
cout << '\t';
-
}
-
-
cout << endl;.
-
}
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.
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
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
by: QWERTY |
last post by:
--------------Boundary-00=_O5I3QL80000000000000
Content-Type: Multipart/Alternative;
boundary="------------Boundary-00=_O5I3LVC0000000000000"
--------------Boundary-00=_O5I3LVC0000000000000...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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.
-->
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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...
| |