Help with semi-commercial C ++ assignment

This is a very simple question, I'm sure, but I would appreciate help. :)

Here is my variable in the .h file:

map<int, map<int, map<int, CString>*>*> batch;

Here I am trying to assign a value:

((*((*(batch[atoi(transnum)]))[1]))[atoi(*docnum)]) = page;

I added a few extra parentheses, trying to figure this out to make sure that the derefs are being processed in the correct order - unfortunately, it still doesn't work. When I run this line, my application just crashes. I have it wrapped up in try {} catch {}, but an exception does not occur. I don't use C ++ very often, and I wonder if anyone can tell me what I'm doing wrong.

Here are the relationships I'm trying to model:

The list of transaction numbers (integers) must be ordered using the key.

For each transaction number, I have two types of documents, payments and invoices (buckets represented by 0 and then 1 respectively in my data structure above)

Each type of bucket can have one or more documents . These documents must be ordered by id (docid)

Each docid refers to a line consisting of a list of comma-delimited files in the file system for processing.

If you think it's better to use a data structure, I would be interested to hear it.

EDIT: , . , MFC- ++ , - . , , , . , . .

+5
7

std::map , node, , . , , , . , , .

? , :

map<int, map<int, map<int, CString> > > batch; // don't forget the spaces

:

batch[atoi(transnum)][1][atoi(*docnum)] = page;
+17

-, typedef , :

typedef std::map<int, CString> page_map;
typedef std::map<int, page_map> document_map;
typedef std::map<int, document_map> batch_map;

batch_map batch;

, , . -, !

int transNumber = atoi(transnum);
int docNumber = atoi(*docnum); // why is docnum a pointer?

batch[transNumber ][1][docNumber] = page;

, , , , .

, . , - .

+13

.

, .

+11

:

map<int, map<int, map<int, CString> > > batch;//no asterisks!

:

batch[atoi(transnum)][1][atoi(*docnum)] = page;
+5

: ?

typedef int transaction_key;
typedef int doc_id;

class Transaction
{
public:

    Transaction(transaction_key key) : m_key(key) {}

    AddPaymentDoc(doc_id, const std::string&);
    AddInvoiceDoc(doc_id, const std::string&);  
    // I'd probably have these methods return a unique ID actually, rather than 
    // create it yourself...  or they can return void and you pass in the doc id.


    // exception handling/other handling for attempting to reference using a bad id
    std::string GetPayment(doc_id);
    std::string GetInvoice(doc_id);

    std::map <doc_id, std::string> GetPayments() {return Payments;}
    std::map <doc_id, std::string> GetInvoices() {return Invoices;}

private:
    transaction_key m_key;
    std::map <doc_id, std::string> Payments;
    std::map <doc_id, std::string> Invoices;    
};
+2

, NULL - . , ( ).

+1

, , , .

std::map- This is an ordered container, so you get the required orders. By avoiding the explicit use of pointers and allowing the container to manage dynamic memory, the model is easier to use and less error prone.

If you have the potential for more types of documents than just payments and invoices, then I can make the document type a transfer and card transaction from the document type to DocumentMap.

#include <map>
#include <string>

// Map of docid to comma separated string of files
typedef std::map<int, std::string> DocumentMap;

struct Transaction
{
    DocumentMap payments;
    DocumentMap invoices;
};

// map of transaction id to transaction contents
typedef std::map<int, Transaction> TransactionMap;

TransactionMap batch;

void foo(TransactionMap& batch)
{
    // ...

    batch[transno].invoices[docno] = page;

    // ...
}
+1
source

All Articles