Thread: Can't understand my error...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    110

    Can't understand my error...

    So I am trying to write this small program using a void method from a source file but it does not work..It is giving me some weird error, that I can't comprehend. My code SEEMS correct but maybe you real programmers out there can help me point out my mistake, as the error is

    Code:
    main.obj : error LNK2019: unresolved external symbol "void __cdecl ordered(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::list<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >)" (?ordered@@YAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$list@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@2@@Z) referenced in function _main
    Here are my 3 files

    Code:
    #include <iostream>
    #include <list>
    #include <string>
    #include "oLine.h"
    #include <vector>
    
    using std::cout;
    using std::endl;
    using std::string;
    using std::cin;
    using std::list;
    
    int main(){
    
    	//asks for user input
    	cout << "Please type in your input: ";
    	
    	//the container
    	list<string> sentence;
    	string x;
    	
    	//adds user input to container
    	while(cin >> x){
    		sentence.push_back(x);
    		}
    	
    	//sorts out sentence
    	list<string> sorted = sentence;
    	sorted.sort();
    	
    	//starts container from beginning, and continues until the end
    	for(list<string>::iterator iter = sorted.begin(); iter != sorted.end(); iter++){
    		ordered(*iter, sentence);
    	}
    
    
    	return 0;
    
    }
    Code:
    #ifndef GUARD_oLine_h
    #define GUARD_oline_h
    
    //oline.h
    
    #include <string>
    #include <list>
    
    void ordered(std::string, std::list<std::string>);
    
    #endif
    Code:
    #include "oLine.h"
    #include <string>
    #include <iostream>
    #include <list>
    
    using std::string;
    using std::cout;
    using std::list;
    
    void ordered(string& start, list<string>& phrase){
    	
    	//counts the number of words outputted
    	list<string>::iterator count = phrase.begin();
    	//current word the iterator is on
    	list<string>::iterator current = phrase.begin();
    	
    	//while the count is not the end
    	while(count != phrase.end()){
    		
    		//if start equals current and current isnt the end
    		//we output current and increment current along with the count
    		if(start == *current && current != phrase.end()){
    			cout << *current << ' ';
    			current++;
    			count++;
    		}
    		
    		//since it is the end, we output current and set it back to the beginning
    		else if(start == *current && current == phrase.end()){
    			cout << *current << ' ';
    			current = phrase.begin();
    			count++;
    		}
    		
    		//otherwise just increment current
    		else{
    			current++;
    		}
    
    	}
    
    }
    Thanks for your help!

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you're declaring it as

    Code:
    void ordered(std::string, std::list<std::string>);
    but defining it as

    Code:
    void ordered(string& start, list<string>& phrase)
    can't have references in one spot and not in the other. also, even with the using statements, you should declare the types the same way in both. either std::x or just x. just do it the same everywhere for the sake of clarity.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Thank you for your reply! So should I have it like this? I actually just tried that and its pretty much giving me the same error.

    Code:
    void ordered(std::string&, std::list<std::string>&);
    
    
    void ordered(string& start, list<string>& phrase)

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    do you compile 2 files together?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You have added all project files to your project?
    Also, don't remove the names of parameters in the prototypes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by dnguyen1022 View Post
    Thank you for your reply! So should I have it like this? I actually just tried that and its pretty much giving me the same error.

    Code:
    void ordered(std::string&, std::list<std::string>&);
    
    
    void ordered(string& start, list<string>& phrase)
    actually it should be more like

    Code:
    void ordered(std::string& start, std::list<std::string>& phrase);
    
    void ordered(std::string& start, std::list<std::string>& phrase)
    {
      // code
    }
    your declaration should always exactly match your definition.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Quote Originally Posted by Elysia View Post
    You have added all project files to your project?
    Also, don't remove the names of parameters in the prototypes.
    Eh..what do you mean by the names of parameters in the prototypes? I am self-teaching myself c++, so the terms are unfamiliar to me.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A prototype is a function declaration, telling the compiler "somewhere, but not here, there is a function X, returning something of type T, and takes parameters ...". It is NECESSARY in a prototype to declare the types of parameters, and what Elysia says is that you should put the names of the parameters too (which isn't strictly required).

    --
    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.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by dnguyen1022 View Post
    Eh..what do you mean by the names of parameters in the prototypes? I am self-teaching myself c++, so the terms are unfamiliar to me.
    That's why I gave you a link. I think it pretty clearly explains the problem and why it's a bad thing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Quote Originally Posted by Elysia View Post
    That's why I gave you a link. I think it pretty clearly explains the problem and why it's a bad thing.
    Sorry about that, I had no idea it was a link lol. I thought you just simply highlighted it in red. I understand now. Thank you!

    However my code is still giving me the same error. I am definitely trying to compile these files together. I even tried to change the name of my header file to see if it was actually attempting to read it. As expected it couldn't find the file. Just in case..here is my coding once again..

    [code]
    #include <iostream>
    #include <list>
    #include <string>
    #include "oLine.h"
    #include <vector>

    using std::cout;
    using std::endl;
    using std::string;
    using std::cin;
    using std::list;

    int main(){

    //asks for user input
    cout << "Please type in your input: ";

    //the container
    list<string> sentence;
    string x;

    //adds user input to container
    while(cin >> x){
    sentence.push_back(x);
    }

    //sorts out sentence
    list<string> sorted = sentence;
    sorted.sort();

    //starts container from beginning, and continues until the end
    for(list<string>::iterator iter = sorted.begin(); iter != sorted.end(); iter++){
    ordered(*iter, sentence);
    }


    return 0;

    }
    [code]

    Code:
    #ifndef GUARD_oLine_h
    #define GUARD_oline_h
    
    //oline.h
    
    #include <string>
    #include <list>
    
    void ordered(std::string&, std::list<std::string>&);
    
    #endif
    Code:
    #include "oLine.h"
    #include <string>
    #include <iostream>
    #include <list>
    
    using std::string;
    using std::cout;
    using std::list;
    
    void ordered(std::string& start, std::list<std::string>& phrase){
    	
    	//counts the number of words outputted
    	list<string>::iterator count = phrase.begin();
    	//current word the iterator is on
    	list<string>::iterator current = phrase.begin();
    	
    	//while the count is not the end
    	while(count != phrase.end()){
    		
    		//if start equals current and current isnt the end
    		//we output current and increment current along with the count
    		if(start == *current && current != phrase.end()){
    			cout << *current << ' ';
    			current++;
    			count++;
    		}
    		
    		//since it is the end, we output current and set it back to the beginning
    		else if(start == *current && current == phrase.end()){
    			cout << *current << ' ';
    			current = phrase.begin();
    			count++;
    		}
    		
    		//otherwise just increment current
    		else{
    			current++;
    		}
    
    	}
    
    }
    Anybody with VS 2008 that could kindly try compiling these files to see what happens? Maybe I may actually be doing something wrong here, which I think is highly possible.
    Last edited by dnguyen1022; 03-10-2009 at 10:04 PM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Compiles and links fine (single source file).
    It's more probably that you did not add your source files to the project. You never answered if you did.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    OK, so I figured out that my problem was not putting it in the correct folders within the solution. I had everything in the same folder, when I should have put the header file in its own folder.

    As for the code, it clearly is not working. It will simply output the first word that is suppose to come out first according to the sort function. It crashes right on the second word. Using the little skills I have with the debugger. It seems like it is having problems when I am incrementing current++ within my source file code.

    This is the code..
    Code:
    #include "oLine.h"
    #include <string>
    #include <iostream>
    #include <list>
    
    using std::string;
    using std::cout;
    using std::list;
    
    void ordered(std::string& start, std::list<std::string>& phrase){
    	
    	//counts the number of words outputted
    	list<string>::iterator count = phrase.begin();
    	//current word the iterator is on
    	list<string>::iterator current = phrase.begin();
    	
    	//while the count is not the end
    	while(count != phrase.end()){
    		
    		//if start equals current and current isnt the end
    		//we output current and increment current along with the count
    		if((start == *current) && (current != phrase.end())){
    			cout << *current << ' ';
    			current++;
    			count++;
    		}
    		
    		//since it is the end, we output current and set it back to the beginning
    		else if((start == *current) && (current == phrase.end())){
    			cout << *current << ' ';
    			current = phrase.begin();
    			count++;
    		}
    		
    		//otherwise just increment current
    		else{
    			current++;
    		}
    
    	}
    
    }
    I think it may be over incrementing, since phrase.end() is actually the one past the last word. I am unsure of how to fix this though. I feel that my if statements are not correctly covering all my cases, and I think that is where I should fix the problem so that it will not over increment. Help greatly appreciated!

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the ordered function supposed to do?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Basically take in a string, and a phrase. Find that string in the phrase, and then output that phrase starting from that string. If that string is in the middle of the phrase, output until it hits the end, and recurse back to the beginning of the phrase and output until count hits phrase.end(), or if count was an int, phrase's size. It should keep recursing as long as count does not equal to phrase.end(). This is kind of vague, so here is an example

    string: blue
    phrase: two blue dogs

    output: blue dogs two

    This is a helper function, and my main code ensures that the string will be in the phrase.

  15. #15
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    So my code is no longer giving me errors but it is not outputting the right stuff. Given the input "1 2 3 4"
    It gives me back
    1 2 3 4
    2 1 3 4
    3 1 2 4
    4 1 2 3

    However it should be
    1 2 3 4
    2 3 4 1
    3 4 1 2
    4 1 2 3

    I can't seem to find where the problem in my code is. I don't see why it is always starting back at 1 after it outputs the first correct output except.
    I created another helper function rLine and I believe that is where the problem lies.

    Code:
    #include <iostream>
    #include <list>
    #include <string>
    #include "oLine.h"
    #include <vector>
    
    using std::cout;
    using std::endl;
    using std::string;
    using std::cin;
    using std::list;
    
    int main(){
    
    	//asks for user input
    	cout << "Please type in your input: ";
    	
    	//the container
    	list<string> sentence;
    	string x;
    	
    	//adds user input to container
    	while(cin >> x){
    		sentence.push_back(x);
    		}
    	
    	//sorts out sentence
    	list<string> sorted = sentence;
    	sorted.sort();
    	
    	//starts container from beginning, and continues until the end
    	for(list<string>::iterator iter = sorted.begin(); iter != sorted.end(); iter++){
    		ordered(*iter, sentence);
    		cout << endl;
    	}
    
    
    	return 0;
    
    }
    Code:
    #include "oLine.h"
    #include <string>
    #include <iostream>
    #include <list>
    #include "rLine.h"
    
    using std::string;
    using std::cout;
    using std::list;
    
    void ordered(std::string& start, std::list<std::string>& phrase){
    	
    	//counts the number of words outputted
    	list<string>::iterator count = phrase.begin();
    	//current word the iterator is on
    	list<string>::iterator current = phrase.begin();
    	
    	//while the count is not the end
    	while(count != phrase.end()){
    
    		if (current == phrase.end()){
    			current = phrase.begin();
    		}
    
    		//if start equals current
    		//we output current and increment current along with the count
    		//and run the method "rest" for the rest of the line
    		if(start == *current){
    			cout << *current << ' ';
    			current++;
    			rest(start, phrase);
    			count = phrase.end();
    		}
    		
    		//otherwise just increment current
    		else{
    			current++;
    		}
    	}
    }
    Code:
    #include "rLine.h"
    #include <string>
    #include <iostream>
    #include <list>
    
    using std::string;
    using std::cout;
    using std::list;
    
    void rest(std::string& start, std::list<std::string>& phrase){
    	
    	//counts the number of words outputted
    	list<string>::iterator count = phrase.begin();
    	//current word the iterator is on
    	list<string>::iterator current = phrase.begin();
    	
    	//while the count is not the end
    	while(count != phrase.end()){
    		
    		//if it reaches the end set it to the beginning
    		if (current == phrase.end()){
    			current = phrase.begin();
    		}
    
    		//if start equals current continue along and increment current and count
    		if(start == *current){
    			current++;
    			count++;
    		}
    		
    		//otherwise output the current and increment current and count 
    		else{
    			cout << *current << ' ';
    			current++;
    			count++;
    			}
    		}
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM