Creating a map with a new object type

This is a discussion on Creating a map with a new object type within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm using the standard map library and I was wondering if it is possible to create a map ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    20

    Question Creating a map with a new object type

    Hi all,

    I'm using the standard map library and I was wondering if it is possible to create a map with a type value created by me (in this case, class "User" type).

    I was looking for something like this:

    Code:
    #include "User.h"
    
    #include <map>
    #include <string>
    
    (...)
    
    map <string, User> users;
    Is this possible? If yes, how?

    Many thanks,
    Joćo

    P.S.: I don't know if it is relevant but I'm not using g++, I'm using Visual C++ 2008 Express Edition since I'll need to design a GUI to this project. I'm saying this because I think sometimes there are some differences between "standard C++ code" and Visual C++ code.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sure, you can use (almost) any type as a first or second object in a map. The first object is a little bit stricter in that it must implement comparison operators (operator == and operator> I think).

    Objects that can't be copied (that has a private copy constructor) can't be stored in a map. Other than that, I don't think there are any further restrictions.

    --
    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
    May 2008
    Posts
    20

    Post

    By the way, the error that Visual C++ 2008 Expression Edition displays is the following (the file is Person.cpp):

    Person.cpp
    C:\Program Files\Microsoft Visual Studio 9.0\VC\include\map(168) : error C3699: '&' : cannot use this indirection on type 'User'
    compiler replacing '&' with '^' to continue parsing
    c:\documents and settings\blacknail\my documents\visual studio 2008\projects\airline\airline\Person.h(13) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
    with
    [
    _Kty=std::string,
    _Ty=User
    ]

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you can post your User class?

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

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    20
    Thanks matsp for the quick reply

    My question is: the way to do it is only through the use of templates or there is another way to use new types in the value field of a map?

    I googled a bit but I obtained somehow contradictory answers.

    Thanks again,
    Jo&#227;o

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    20
    matsp here it goes, thanks again.

    It's a very simple class.

    User.h

    Code:
    #pragma once
    
    ref class User
    {
    public:
    	User(void);
    
    	void setUserId(int id);
    	void setUserName(System::String^ name);
    	void setUserAddress(System::String^ address);
    	void setUserContacts(System::String^ contacts);
    	void setAcomulatedMiles(int miles);
    	void setUserFunction(System::String^ function);
    	void setUserType(System::String^ type);
    
    	int getUserId();
    	System::String^ getUserName();
    	System::String^ getUserAddress();
    	System::String^ getUserContacts();
    	int getAcomulatedMiles();
    	System::String^ getUserFunction();
    	System::String^ getUserType();
    
    protected: 
    	int userId;
    	System::String^ userName;
    	System::String^ userAddress;
    	System::String^ userContacts;
    	int acomulatedMiles;
    	System::String^ userFunction;
    	System::String^ userType;
    };
    User.cpp

    Code:
    #include "StdAfx.h"
    #include "User.h"
    
    User::User(void)
    {
    	this->userId = 0;
    	this->userName = "";
    	this->userAddress = "";
    	this->userContacts = "";
    	this->acomulatedMiles = 0;
    	this->userFunction = "";
    	this->userType = "";
    }
    
    void User::setUserId(int id) {
    	this->userId = id;
    }
    
    void User::setUserName(System::String^ name) {
    	this->userName = name;
    }
    
    void User::setUserAddress(System::String^ address) {
    	this->userAddress = address;
    }
    
    void User::setUserContacts(System::String^ contacts) {
    	this->userContacts = contacts;
    }
    
    void User::setAcomulatedMiles(int miles) {
    	this->acomulatedMiles = miles;
    }
    
    void User::setUserFunction(System::String^ function) {
    	this->userFunction = function;
    }
    
    void User::setUserType(System::String^ type) {
    	this->userType = type;
    }
    
    int User::getUserId() {
    	return this->userId;
    }
    
    System::String^ User::getUserName() {
    	return this->userName;
    }
    
    System::String^ User::getUserAddress() {
    	return this->userAddress;
    }
    
    System::String^ User::getUserContacts() {
    	return this->userContacts;
    }
    
    int User::getAcomulatedMiles() {
    	return this->acomulatedMiles;
    }
    
    System::String^ User::getUserFunction() {
    	return this->userFunction;
    }
    
    System::String^ User::getUserType() {
    	return this->userType;
    }

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    20
    I solved my problem, thanks! It had to do with the Managed code I had... I deleted that code and now I'm using only non-Managed code.

    Now I&#180;ve changed the code to:

    Code:
    (..)
    map <string, User*> users;
    (..)
    ...and (at least) it compiles!

    Thanks for all again,
    Jo&#227;o

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  2. Replies: 4
    Last Post: 11-14-2006, 11:52 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 05:24 PM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM

Tags for this Thread


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