string redeclaration in function, why is required?

This is a discussion on string redeclaration in function, why is required? within the C++ Programming forums, part of the General Programming Boards category; Code: int main(int argc, char* argv[]){ argc--; if ( argc < 1){ cout << "Usage:" << endl << "h5inspect file.h5" ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    106

    string redeclaration in function, why is required?

    Code:
    int main(int argc, char* argv[]){
    	argc--;
    	if ( argc < 1){
    		cout << "Usage:" << endl << "h5inspect file.h5" << endl;
    		return 0;
    	}
    	//iterate *.h5 in ascending order
    	for (int count = 1; count <= argc; count ++){ 
    		
    		char iterchar; 
    		string filename = argv[count]; 
    		cout << "HDF5 \"" << filename << "\" { "<< endl;
    		dataset_loader dset(string(filename));
    The last line of code is of interest. The string filename is already declared as a string, but the function only works when the parameter is given as "string(filename)" or "string& filename". But filename is already defined as a string; why does it still required the string 'filename' to be made a string, again?!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by elninio View Post
    Code:
    int main(int argc, char* argv[]){
    	argc--;
    	if ( argc < 1){
    		cout << "Usage:" << endl << "h5inspect file.h5" << endl;
    		return 0;
    	}
    	//iterate *.h5 in ascending order
    	for (int count = 1; count <= argc; count ++){ 
    		
    		char iterchar; 
    		string filename = argv[count]; 
    		cout << "HDF5 \"" << filename << "\" { "<< endl;
    		dataset_loader dset(string(filename));
    The last line of code is of interest. The string filename is already declared as a string, but the function only works when the parameter is given as "string(filename)" or "string& filename". But filename is already defined as a string; why does it still required the string 'filename' to be made a string, again?!
    I doubt it does. Do you get an error if you remove it?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    106
    of course I do!

    Code:
    /tmp/cc6ghKxc.o: In function `main':
    h5inspect.cpp:(.text+0x125): undefined reference to `H5check_version'
    h5inspect.cpp:(.text+0x12c): undefined reference to `H5::FileAccPropList::DEFAULT'
    h5inspect.cpp:(.text+0x131): undefined reference to `H5::FileCreatPropList::DEFAULT'
    h5inspect.cpp:(.text+0x13d): undefined reference to `H5::H5File::H5File(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, H5::FileCreatPropList const&, H5::FileAccPropList const&)'
    h5inspect.cpp:(.text+0x1f1): undefined reference to `H5::H5File::close()'
    h5inspect.cpp:(.text+0x201): undefined reference to `H5::H5File::close()'
    h5inspect.cpp:(.text+0x237): undefined reference to `H5::H5File::~H5File()'
    h5inspect.cpp:(.text+0x247): undefined reference to `H5::H5File::~H5File()'
    h5inspect.cpp:(.text+0x283): undefined reference to `H5::H5File::~H5File()'
    h5inspect.cpp:(.text+0x291): undefined reference to `H5::H5File::~H5File()'
    collect2: ld returned 1 exit status

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    What are the constructors of dataset_loader? Is dataset_loader a typedef for a template?

    I can imagine something like this happening if the string constructor call causes a different template constructor be called. Using a string constructor like that actually evaluates to a const string. If there are a different template constructors for const and non const strings, then there will be a difference in which constructor is called. But to make undefined references to appear just by calling the constructor requires that the constructor or it's class be a template.

    But that would be a very obscure occurrence.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    106
    dataset_loader is neither a namespace or a template

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    You are hiding information about your code. Provide a small but complete example that exhibits the behaviours of interest.

    My bet is that dset is a macro. I wouldn't be surprised if dataset_loader and string are also macros with different meanings from what people would normally assume by looking at your code.

    The only other explanation for getting linker errors is that you are linking against different libraries or objects when you change the code.

    Either way, you are providing information that is incomplete, but just enough to deceive.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    106
    Got it, added debug flag to g++. Turns out I was missing libs, the includes were there but the lib directory was wrong. Sorry if I wasn't providing enough information, I would have to send you the files to tell more! I did not write these files, I have received them from a colleague, so I do not know much about them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM

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