Reputation: 3381
I'm new to C++ and I have this problem that the compiler is throwing at me whenever I compile my program. From what I've learnt so far you have a class in which you declare constructors and functions. Then after the class you implement the functions. Then in the main you call/manipulate them. I was writing a linkedlist program but I kept getting this annoying compiler error:
error: request for member
'get_number'
in'test'
, which is of non-class type'LinkList*'
My original program was quite longer than the below, but as I kept getting the above error I wrote a really simple program just to see where the error was being thrown. Despite writing it very simple I got the EXACT same error. So I'm doing something wrong when I'm calling the function that I have declared and implemented.
If someone could just help me point out what I'm doing wrong and why I can't call that function:
#include <iostream>
using namespace std;
class LinkList
{
public:
int result;
//declaring the constructor
LinkList(int i);
//declaring simple function
int get_number();
};
//Implementing the constructor
LinkList::LinkList(int i)
{
result = i;
};
//implementing test function
int LinkList::get_number()
{
return result;
}
int main()
{
LinkList *test = new LinkList(5);
int getting_number = 0;
//Error trigger:
test.get_number();
cout<<getting_number;
return 0;
}
Upvotes: 2
Views: 268
Reputation: 31435
Ok, I will say "stop right there". This is not Java and it is not C#. It is C++. In Java and C# you always create your objects with new.
In C++ you don't always create them that way.
Your better code would be simply:
int main()
{
LinkList test(5);
int getting_number = test.get_number();
std::cout<<getting_number << '\n';
return 0;
}
Upvotes: 0
Reputation: 258568
Objects in C++ are either in automatic storage or dynamic storage (there's also static storage, but that's beyond the scope of the question).
When you create an object with new
, you create it in dynamic storage and return a pointer to it.
Members of an object referenced by a pointer are accessed with the ->
operator:
test->get_number();
which is equivalent to:
(*test).get_number();
You also have a memory leak in your program, as the memory allocated for test
is never freed. You need to explicitly call delete test
to free it. (in this case it's not an actual memory leak since the system reclaims the memory on the program exit, but you should know this for the future).
In this case you should however use automatic storage objects:
LinkedList test(5);
//...
test.get_number();
This way, you don't need to worry about memory management and it's also preferable to use automatically managed objects instead of dynamic ones.
In c++, non-pod types are initialized before entering the constructor body, so you should start using initializer lists:
LinkList::LinkList(int i) :
result(i)
{
};
This prevents double initialization and is an easy optimization trick. Even if for this particular case the difference is not noticeable, you should start getting into the habit of initializing members like this early on.
Upvotes: 3
Reputation: 2474
just use
LinkedList test(5);
instead of
LinkList *test = new LinkList(5);
if you insist on using the new variant you need to change the
test.get_number();
to
test->get_number();
and free (delete) your test object before exiting the function;
and even then you are not assigning the return value from get_number to anything, so your getting_number varaible is still 0;
Upvotes: 3
Reputation: 33167
The error is correct. test
is a LinkedList*
, a pointer to a LinkedList
. You must call it with test->get_number()
. The dot-call is only for non-pointer types.
Upvotes: 0
Reputation: 8004
test
is not a LinkList
itself but a pointer to one. Therefore,
test.get_number();
should be:
test->get_number();
This is just a shortcut for:
(*test).get_number();
Upvotes: 0
Reputation: 329
Test is a pointer to a LinkList object, you have to access properties and members on test using the arrow operator (->), for example: test->get_number();
Or you could just not create a pointer to the object and create a normal object.
Upvotes: 4