Title: Calling a method of a class in C#
Tags: c++;qt
Question: Apologies if this is really stupid, but I just can't figure out how to do this.
I have been programming in C# for ages. Today I thought I'd try some C++ for the very first time and installed Qt for windows.
I am trying to write a console application which outputs results from the Project Euler problems.
Having managed to output string values to the console via cout I thought let's make a class and have the problems as static methods, just like I would do in C#... but I can't figure out how to call the class I have created. And over an hour of searching the web has giving me nothing useful so far.
I have:
#include <iostream&gt;
using namespace std;
void main()
problems p = new problems ();
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << "Hello" << "\n";
Which absolutely doesn't work. I guess because I need some using or include statement to give access to the class...
The class header:
#ifndef PROBLEMS_H
#define PROBLEMS_H
class problems
public:
static long problem1();
#endif // PROBLEMS_H
and class cpp:
long problems75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0problem1()
long output = 0;
for (int i = 1; i < 1000; i++)
if (i % 3 == 0 || i % 5 == 0)
output += i;
return output;
I don't get any errors in the class declaration, so I guess that must be correct (but I'm not sure of course...). Ultimately I would like to print the output of problem1() instead of "Hello" in main()
How can I make this work?

Here is the accepted answer: Edit this answers the original question, where the problems75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0problem1 method was static.
You do not need an instance to call a static method:
#include <iostream&gt;
#include "header_with_problems_declaration.h"
void main()
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << problems75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0problem1() << "\n";
For a non-static method, you would do something like
#include <iostream&gt;
#include "header_with_problems_declaration.h"
void main()
problems p
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << p.problem1() << "\n";
Note that there has been no dynamic allocation with new. There seems to be no reason to do that in this case.
You also need to include the relevant header, compile the implementation, and link it into the main. How to do this depends on your build system.
In C++, unless a static method is really related to a class, you'd put a non-member function inside a namespace instead:
namespace problems
long problem1() { ..... }
Comment for this answer: Thanks, sorry, I forgot to correct that after I changed the method to static. It's the same in C#. But this doesn't work.
Comment for this answer: Ah, I needed quotation marks on the include!! Thanks, I was using <> as for the iostream include. Now it works.
Comment for this answer: I reverted my change to the code to make it consistent with this answer. The bit I was really missing was the `include "header.h"`, which this answer provided.
Comment for this answer: I won't do it again. Thanks for the heads up on the use of `new`. I guess it's because there is no automatic garbage collection in C++, right?
Comment for this answer: @user643192 how does it not work? BTW you are missing the include with the `problems` class declaration in our main.
Comment for this answer: @user643192 OK, it is good not to change the questions halfway through. Also, pay special attention to the non-use of `new`. Often programmers coming from other language to C++ over-use `new`. You should only use it if strictly necessary.
Comment for this answer: @user643192 that is one reason. You have to manage the memory by yourself. Another is that you can allocate objects on the stack, which you can't do in e.g. Java (not sure about C#).

Here is another answer: In order to include the .h file and to have access to the class you need #include:
#include "problems.h"
New creates a pointer so you need to access the method through the pointer:
problems *p = new problems ();
long x = p-&gt;problem1();
You can also access it like this:
problems p = problems();
long x = p.problem1();
Comment for this answer: @juanchopanza Take a look at the question after it was modified. It actually isn't
Comment for this answer: Sorry guys, I modified the question code after juanchopanza pointed out my error. I chose to make the code consistent with the way main() was coded. You are both right and this answer as well showed me useful stuff for coding in C++.
Comment for this answer: The include part is right, but the method is static so the rest is wrong.
Comment for this answer: OK, removed my downvote. The meaning of the question changed significantly!

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:11/20/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c++><qt>', 'Title': 'Calling a method of a class in C#', 'ViewCount': 96, 'id': 13467655, 'user_AboutMe': '<p>Civil engineer trying to be a programmer.</p>\n', 'user_AccountId': 323277.0, 'user_DisplayName': 'yu_ominae', 'user_Reputation': 2925.0, 'user_Views': 471.0, 'user_id': 643192.0}}