-
February 3rd, 2013, 04:58 AM
#1
error C2440: '=' : cannot convert from 'const char *' to 'char *
Hi,
I want to do following , but it gives error C2440: '=' : cannot convert from 'const char *' to 'char *.
............
string str("VRML");
char *sss[];
sss[0]=str.c_str();
cout<<"SSS:"<<sss[0]<<endl;
............
Is there anybody who have experienced this problem?
-
February 3rd, 2013, 06:19 AM
#2
Re: error C2440: '=' : cannot convert from 'const char *' to 'char *
Originally Posted by nihad
Hi,
I want to do following , but it gives error C2440: '=' : cannot convert from 'const char *' to 'char *.
Code:
string str("VRML");
char *sss[];
sss[0]=str.c_str();
cout<<"SSS:"<<sss[0]<<endl;
Is there anybody who have experienced this problem?
Before fixing the "convert" error you first have to fix your other problem: you are trying to access the first element of the char *sss[] array while this array is empty (there is not any element at all!)
BTW, what are you trying to achieve with this code?
Victor Nijegorodov
-
February 3rd, 2013, 08:33 AM
#3
Re: error C2440: '=' : cannot convert from 'const char *' to 'char *
Actually this is my code: I use command line argument, argv[1] is my one of the input file name. I want to change argv[2] and argv[3] dynamically inside the for loop.
int main(int argc, char *argv[])
{
mf::Mesh mesh;
mf::Reader reader;
mf::Writer writer;
mf::Normalizer normalizer;
int n;
if (argc != 2) {
std::cerr << "usage: " << argv[0] << " [Input OBJ Path] " << std::endl;
exit(EXIT_FAILURE);
}
/////// New Code Start/////////////
FILE *fp;
fp=fopen(argv[1],"r");
unsigned int i,j;
unsigned int number_of_lines = 0;
unsigned int count_row=0;
double *read_test_off_file;
std::string line;
std::ifstream myfile(argv[1]);
while (std::getline(myfile,line))
{
++number_of_lines;
}
std::cout << "Number of lines in text file: " << number_of_lines<<endl;
read_test_off_file=new double[number_of_lines];
for(i=0;i<number_of_lines;i++)
{
fscanf(fp,"%lf",&read_test_off_file[i]);
}
int test=0;
cout<<"Scanf Complete."<<endl;
for(j= 0;j<number_of_lines;j++)
{
cout<< j << ":" << read_test_off_file[j] << endl;
std::string first_part("m");
std::string second_part;
std:stringstream ss;
ss << read_test_off_file[j];
second_part = ss.str();
std::string normalize_part("_normalize");
std::string last_part(".off");
std::string input_file_name=first_part+second_part+last_part;
argv[2]=input_file_name.c_str();
std::string output_file_name=first_part+second_part+normalize_part+last_part;
argv[3]= output_file_name.c_str();
if (reader.read_off(mesh, argv[2]) == false)
{
exit(EXIT_FAILURE);
}
normalizer.triangulation(mesh);
normalizer.translation(mesh);
normalizer.scaling(mesh);
normalizer.rotation_point_svd(mesh, 16500);
writer.write_off(mesh, argv[3]);
test++;
}
....................................................
-
February 3rd, 2013, 09:16 AM
#4
Re: error C2440: '=' : cannot convert from 'const char *' to 'char *
Originally Posted by nihad
Actually this is my code: I use command line argument, argv[1] is my one of the input file name. I want to change argv[2] and argv[3] dynamically inside the for loop.
First, use code tags when posting code. The code you posted is practically unreadable.
Second, you should not be attempting to change argv[] directly. Just copy the values of argv[] to your own local string variables and manipulate those local strings. There is no guarantee that argv[] is modifiable, and even they are modifiable by chance, modifying them is undefined behaviour.
Regards,
Paul McKenzie
-
February 3rd, 2013, 09:38 AM
#5
Re: error C2440: '=' : cannot convert from 'const char *' to 'char *
Finally, I did it. It will be writer.write_off(mesh, output_file_name.c_str()); instead of argv[3]= output_file_name.c_str();
-
February 3rd, 2013, 09:47 AM
#6
Re: error C2440: '=' : cannot convert from 'const char *' to 'char *
Originally Posted by nihad
Finally, I did it. It will be writer.write_off(mesh, output_file_name.c_str()); instead of argv[3]= output_file_name.c_str();
And what about this erroneous line?
Code:
argv[2]=input_file_name.c_str();
Did you change that to not modify argv[2]?
Regards,
Paul McKenzie
-
February 3rd, 2013, 09:50 AM
#7
Re: error C2440: '=' : cannot convert from 'const char *' to 'char *
Yes. It would be reader.read_off(mesh, input_file_name.c_str()) instead of argv[2]=input_file_name.c_str().
-
February 3rd, 2013, 09:58 AM
#8
Re: error C2440: '=' : cannot convert from 'const char *' to 'char *
Originally Posted by nihad
Yes. It would be reader.read_off(mesh, input_file_name.c_str()) instead of argv[2]=input_file_name.c_str().
Also, your code has a memory leak:
Code:
read_test_off_file=new double[number_of_lines];
Where is the delete[] for this call to new[]?
But in general, coding like this in this day and age of C++ is not necessary. You have CArray if using MFC, and std::vector<> for dynamic arrays, removing the need for new[]/delete[].
Code:
#include <vector>
//..
std::vector<double> read_test_off_file;
//...
read_test_off_file.resize(number_of_lines);
//...
Then when you need to pass a double*, just pass the address of the first element of the vector.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; February 3rd, 2013 at 10:01 AM.
-
February 3rd, 2013, 10:04 AM
#9
Re: error C2440: '=' : cannot convert from 'const char *' to 'char *
More issues:
Code:
FILE *fp;
fp=fopen(argv[1],"r");
You never checked to see if fp is NULL. What if that file doesn't exist?
Regards,
Paul McKenzie
-
February 3rd, 2013, 10:21 AM
#10
Re: error C2440: '=' : cannot convert from 'const char *' to 'char *
Ya, I should include some code for checking.
-
February 3rd, 2013, 10:58 AM
#11
Re: error C2440: '=' : cannot convert from 'const char *' to 'char *
Originally Posted by nihad
Actually this is my code: I use command line argument, argv[1] is my one of the input file name. I want to change argv[2] and argv[3] dynamically inside the for loop.
Why change argv[2] and argv[3]?
Don't ever try!
Instead save their values in string variables (or in a vector of string objects) and only after this change them dynamically!
Victor Nijegorodov
-
February 3rd, 2013, 02:17 PM
#12
Re: error C2440: '=' : cannot convert from 'const char *' to 'char *
Originally Posted by nihad
Hi,
I want to do following , but it gives error C2440: '=' : cannot convert from 'const char *' to 'char *.
............
string str("VRML");
char *sss[];
sss[0]=str.c_str();
cout<<"SSS:"<<sss[0]<<endl;
............
Is there anybody who have experienced this problem?
First of all, this code gives two errors, not one. And the omitted error is much more interesting.
Code:
D:\Temp\77>cl 77.cpp /EHsc
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
77.cpp
77.cpp(9) : error C2133: 'sss' : unknown size
77.cpp(10) : error C2440: '=' : cannot convert from 'const char *' to 'char *'
Conversion loses qualifiers
Here you need to understand what is the difference between the following definitions in C/C++:
Code:
char* sss0[ ]; // this just defines a pointer to an array of char* of unknown size, results in no array allocation!
char* sss1[1]; // this defines an array of char* of size 1, results in array allocation
Second error means that pointer to non-constant string is assigned with address pointing to constant string. Using non-constant pointer to constant memory may result in constant object being modified, which conflicts with meaning of being constant. No such conversion exists that would keep the referred object be constant. Therefore you need to explicitly make a modifiable object copy instead of casting pointers. Or make the lvalue be of the type compatible with the type of rvalue.
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str("VRML");
const char *sss[1];
sss[0]=str.c_str();
cout<<"SSS:"<<sss[0]<<endl;
return 0;
}
Code:
D:\Temp\77>cl 77.cpp /EHsc
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
77.cpp
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:77.exe
77.obj
Code:
D:\Temp\77>77.exe
SSS:VRML
Last edited by Igor Vartanov; February 3rd, 2013 at 02:22 PM.
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|