Help: can't get xcode to compile a class

Discussion in 'Mac Programming' started by obooyo, Jul 28, 2008.

  1. obooyo macrumors newbie

    Joined:
    Jul 22, 2008
    #1
    I get multiple errors whenever I try to compile this header file, and I don't see any problem with it. Some of the errors are, "expected ')' before 'b'", referring to the second constructor, and "string does not name a type", referring to the accessor.

    Code:
    #include <string>
    #include <vector>
    
    struct amount
    {
    	int longAmount;
    	int shortAmount;
    	double delimiter;
    };
    
    struct price
    {
    	double bid;
    	double ask;
    };
    
    
    class pair
    	{
    	public:
    		//contructor
    		pair();
    		pair(string b, string q, price p, vector<amount> &chart);
    		pair(string b, string q, vector<amount> &chart);
    		
    		//modifier
    		
    		
    		//accessors
    		string baseCurrency() const;
    		string quoteCurrency() const;
    		double price() const;
    		vector<amount> amountChart() const;
    		vector<int> currentLongPos() const;
    		vector<int> currentshortPos() const;
    		
    	private:
    		string base;
    		string quote;
    		price currentPrice;
    		vector<amount> chart;
    		vector<int> longPos;
    		vector<int> shortPos;
    		
    		
    	};
    
    I don't really understand the way Xcode link files. It seems like even if the project does not include this file, I can still just use #include command to include the file. If that is the case, then what does this project thing do? What I would prefer is actually to have different main.cpp, one of which is the program I want to write, and the other one to be used as a testing program for the classes. How do I do that? Should I create two projects?
     
  2. EliseVanLooij macrumors newbie

    Joined:
    Jul 28, 2008
    #2
    Include in target

    Can't help you with all the C, but when you say XCode doesn't even see the file, check whether it's included in the target (red, target-like icon). Just drag the file to the Compile Sources fase, hit Build & Run and enjoy, or not as the case may be.
     
  3. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #3
    You need to use the std namespace, either by specifying it dierectly, eg std::string, or with a declaration:-

    using namespace std ;

    A .h file doesn't have to be part of the project, but if it is then Xcode will keep track of which files depend on it and recompile them on the next build if you modify the .h file.

    If you want to have two main() functions in you project, the best thing to do is to create separate files, targets and executables in your project. There are drop down menus in the tool bar to select which is the current target (the target you are compiling) and the current executable (the one which is run when you hit run). You need to make sure you put the files, eg main1.cpp and main2.cpp, into their respective targets. If you include both in the same target then you'll get a duplicate symbol for __main.

    b e n
     
  4. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #4
    Add the "std" namespaces before your standard types (string and vector), e.g.

    Code:
    pair(std::string b, std::string q, price p, std::vector<amount> &chart);
    pair(std::string b, std::string q, std::vector<amount> &chart);
    
    Alternatively, you can add

    Code:
    using namespace std;
    
    at the top of your cpp file.

    This is not a link issue.

    If you try adding two main.cpp files to your project, you will run into link issues, because any executable may have only a single main() function.

    Hope this helps,
    Sander
     
  5. obooyo thread starter macrumors newbie

    Joined:
    Jul 22, 2008
    #5
    So I added the using namespace std line. But now I get some different errors. The first one is: "String: No such file or directory", referring to the first line #include <string>. Here's the new header file:
    Code:
    #include <string>
    #include <vector>
    
    using namespace std;
    
    struct amount
    {
    	int longAmount;
    	int shortAmount;
    	double delimiter;
    };
    
    struct price
    {
    	double bid;
    	double ask;
    };
    
    
    class pair
    	{
    	public:
    		//contructor
    		pair();
    		pair(string b, string q, price p, vector<amount> &chart);
    		pair(string b, string q, vector<amount> &chart);
    		
    		//modifier
    		
    		
    		//accessors
    		string baseCurrency() const;
    		string quoteCurrency() const;
    		double currentPrice() const;
    		vector<amount> amountChart() const;
    		vector<int> currentLongPos() const;
    		vector<int> currentshortPos() const;
    		
    	private:
    		string mybase;
    		string myquote;
    		price mycurrentPrice;
    		vector<amount> mychart;
    		vector<int> mylongPos;
    		vector<int> myshortPos;
    		
    		
    	};
    
     
  6. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #6
    How did you create your project? Did you select C++ tool from the 'new project' templates?

    b e n
     
  7. obooyo thread starter macrumors newbie

    Joined:
    Jul 22, 2008
    #7
    Yeah I did. All three files are in the source directory. The main.cpp starts like this:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    #include "pair.h"
    
    using namespace std;
    
    The header file starts with this:
    Code:
    #include <string>
    #include <vector>
    
    using namespace std;
    
    The .c file starts with this:
    Code:
    #include "pair.h"
    
    I really don't see what the problem is. How can it not recognize the string object?? In fact, I tried g++ using the terminal, and it compiles. So I believe this is really an Xcode-specific type of problem.
     
  8. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #8
    Ah I think perhaps you're trying to send a C++ header file through the C compiler. .c files will be compiled using the C compiler, .cpp file with the C++ compiler and .mm files with the Objective-C++ compiler.

    So try renaimng your .c file to .cpp. The reason why it compiles from the cl is that you are invoking a C++ compiler by hand rather than relying on suffix recognition.

    b e n
     
  9. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #9
    It's better not to put this "using namespace" in a header file.

    As to your real problem, it's probably like lazydog said: You have a .c file which is really a C++ file; the compiler uses the file extension to "guess" that it is supposed to compile this as C. It then has different include paths and can't find the "string" header file. And even if it could, it would become very confused later on by the rest of the (C++) code.

    Cheers,
    Sander
     
  10. obooyo thread starter macrumors newbie

    Joined:
    Jul 22, 2008
    #10
    As suggested, I made a cpp file for the implementation of the class. But now I still get tons of errors.

    pair.h:
    Code:
    
    #ifndef _pair_included_
    #define _pair_included_
    #include <string>
    #include <vector>
    
    using namespace std;
    
    struct amount
    {
    	int longAmount;
    	int shortAmount;
    	double delimiter;
    };
    
    struct price
    {
    	double bid;
    	double ask;
    };
    
    
    class pair
    {
    .....		
    };
    
    #endif
    
    pair.cpp:
    Code:
    #include "pair.h"
    
    const int DEFAULT_POSITION_TABLE_SIIZE = 30000;
    
    pair::pair()
    {
    }
    
    .....
    
    main.cpp:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    #include "pair.h"
    
    using namespace std;
    
    
    main()
    {
    ....
    }
    
    The first error says, "pair" has not been declared. This is referring to the pair.cpp file, the first default constructor of pair. It seems like most of the errors have something to do with not recognizing stuff written in the header file. Any ideas?
     
  11. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #11
    Does your project still compile without errors from the command line?

    b e n
     
  12. JVene macrumors newbie

    Joined:
    Jul 22, 2008
    #12
    Just in case, change the name pair to something else.

    pair is a template class in the STL, and perhaps your STL implementation is referring to it already.

    That would create a number of issues, especially if pair is declared by not defined in the STL headers you're including, and then it finds pair in this class declaration where it also expects both a definition of the template before proceeding, and template parameters required by pair.
     
  13. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #13
    Well spotted, though it would have been okay if the header file hadn't used the std namespace. Sander's earlier words of wisdom have been demonstrated to be true!

    b e n
     
  14. obooyo thread starter macrumors newbie

    Joined:
    Jul 22, 2008
    #14
    I changed the name of the class and now it works. Thanks guys.
     

Share This Page