Thread: Finding the name of the object parameter

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    28

    Finding the name of the object parameter

    I am writing a class for matrices by the name of Matrix. I have an empty constructor Matrix() where I initialize the dimensions (rows and columns) of the matrix to zero. This is because I would like the user in the main program to set the dimensions through another overloaded constructor or other explicit means. However, in case the user fails to set the dimensions of the object of class Matrix, the dimensions will remain at 0. I have written the following function that checks for undefined dimensions that works:

    Code:
    	void dimension_check(const Matrix &mat2) const {
    		if ((mat2.rows==0)&&(mat2.columns==0)) {
    			cout << "Error. Matrix dimensions undefined.\n";
    			exit(1);
    		}
    		return;
    	}
    I would like to make a change where it will print an error output giving the name of the object that has not been defined.

    i.e in the main program if I define

    Code:
    Matrix ABC;
    The error message should read "Error. Matrix ABC dimensions undefined." Thus the user will know where the error is.

    Thanks.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That's impossible. C++ does not retain the names of local variables (or anything really, from the viewpoint of the program code) at runtime.

    The only thing you can do is pass the name explicitly. But that's considerably more effort than it's worth.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    28
    Thanks CornedBee. I'll give up on this one.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A better idea is to place an ASSERT instead.
    You don't want the user to see that, do you? With an ASSERT, then program will break when the condition is met and you can take a look at the stack to see in what object the problem occurred.
    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.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    A better idea is to place an ASSERT instead.
    You don't want the user to see that, do you? With an ASSERT, then program will break when the condition is met and you can take a look at the stack to see in what object the problem occurred.
    I think ASSERT() is Windows specific.
    The standard assert() is defined in <cassert>

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    ASSERT (uppercase) is MFC I believe. But the name of it is the same, it's just how you write it that's different.
    So yes, assert from cassert is what you should use.
    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.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is entirely possible to do this with a macro (just like assert works):

    Code:
    #include <iostream>
    
    #define ASSERT_DIMENSION(x) \
        do \
        { \
            if (!x.assert_dimension()) \
            { \
                std::cout << "Dimension is not set for " << #x << '\n'; \
            } \
        } while(0)
    
    class Matrix1
    {
        public:
            Matrix1(int n = 0): d(n) {}
            bool assert_dimension() const { return d != 0; }
        private:
            int d;
    };
    
    int main()
    {
        Matrix1 a(32), b;
        ASSERT_DIMENSION(a);
        ASSERT_DIMENSION(b);
    }
    Of course, there's no need to roll your own (probably broken) assert when there's a perfectly good assert (and there's no need for a function dimension_check as the condition to assert is very simple).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    28
    Thanks anon, Elysia and cpjust. I'll read up more on this assert().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my error is storage class specified for parameter
    By meli in forum C Programming
    Replies: 5
    Last Post: 03-27-2009, 12:06 PM
  2. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  3. Linked List Templates and Object Types
    By ventolin in forum C++ Programming
    Replies: 10
    Last Post: 06-16-2004, 12:05 PM
  4. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 02:56 PM