I am having issues with my game. I haven't made much of a game so far, but I am trying to call a string using a external function from my bin class. When I compile, It says I can't have non-static variables.
#include <iostream>
#include <stdlib.h>
using namespace std;
class Bin {
string gameStart = "How would you like to start?\n";
};
Bin bin1;
int main () {
cout << bin1.gameStart;
}
Just for reference, I have tried looking for solutions, but to no prevail.
Update: Hi again. I have read some of the feedback from my question. I see that Raw N and Angew have made some valid points. I would like to ask if these two nice people: Where does the header go. How can i get that version of the compiler?
Thanks!
Update: I managed to find out how to compile my code in c++14. There is a console command that can be used for this. I updated my open.bat file, which i use to compile my notepad code. Everything is working fine now. Thanks all!
The default for a class' members is private, so without specifying differently, your data will not be accessible from outside the class.
Add public: inside the class, before the declarations.
Related
this question looks familiar but trust me it actually is not ! Let me explain why. This is not about saving a function in a header file and including and calling it. An example will illustrate my query clearly so here we go: I have a program like the following:
#include whatever_should_be_included
int main()
{
whatever-1
whatever-2
whatever-3
.
.
.
}
All those whatevers are codes in the program, including if conditions, loops and really whatever. I want those "whatever"s to be saved in files, let's say plain text files or whatever extension is necessary, let's say "exten" be the extensions, then I want to save them as:
chunk-1.exten, chunk-2.exten, .... etc files that will contain: whatever-1, whatever-2,... etc chunks of lines of codes and my program now should look like:
#include whatever_should_be_included
#include those_chunks_maybe
int main(){
chunk-1.exten; //or whatever syntax necessary
chunk2.exten;
chunk-3.exten;
.
.
.
}
I am a beginner in C++ and in programming in general so please go easy on me :) a step by step clear answer with a bit of explanation will be really appreciated.
Edit-1:
I am using Ubuntu 16.04
My compiler is g++ although I am not directly using it, I am compiling or rather loading the programs inside the CERN's ROOT shell, as root macros.
Edit-2:
Of course a better way to go is to use functions and headers, but that does not mean that we cannot explore this feature of chunking out plain code-texts to different files and include them inside the main ! I wanted to learn this and I don't understand how learning this (however dull) feature is wrong !! Why voting negative exactly ? Is this question harming people ? Is it not curious and promote knowledge ?
I'll say a little about how you can, but first I'll say you probably shouldn't. I don't know your application and possibly you really do have a good reason, but this runs against the typical grain of the language and is more likely to cause problems with readability and maintenance than to solve whatever underlying concern is driving the question...
That said, in C++ directives that start with # are for a "pre-processor" which modifies the code prior to compilation. In particular #include copies the content of another file in place of the directive so everything gets compiled together. It is usually (and IMO most correctly) used to pull in headers, but it can be used for anything.
By convention you're correct in thinking that your "chunk" files should not end in .c, or .cpp, or .h; because those imply certain things about the structure of what's inside. You can make up an extension I guess, as long as it doesn't conflict with any standard.
So if you have chunk1.cfragment or something like that, and it contians your whatever-1, then the top-level .cpp file would look like
#include whatever_should_be_included
int main(){
#include "chunk1.cfragment";
#include "chunk2.cfragment";
#include "chunk3.cfragment";
. . .
}
I don't recall (and it may depend on your compiler) whether you can have whitespace at the start of the line before the #include directive; if not that's another way your code will end up ugly (but far from the worst problem with readability here).
Note that you use quotes for the included filename here, not angle brackets as you would with a system library.
UPDATE - Based on your comments on Robert's answer, it seems what you're really after is globally visible/modifiable variables. Again I'll say that this is not usually the best thing, but if you need it there are better ways to get it than by gluing together code fragments into a single massive function.
Better than creating global data, usually, is to pass parameters between functions as needed. But if you do have data that is needed literally everywhere, you can do it with global variables.
In general functions can share access to global variables - those declared outside of any function. To share them across code files, you need to provide extern declarations in all files (and the actual variable definition in exactly one code file). This is best managed with headers.
It's been a long time, so I may have some details a bit off here, but it would look more or less like this:
File1.h
===========================================
extern int a;
===========================================
File2.h
===========================================
void otherFunction();
===========================================
File1.cc
===========================================
#include "File1.h"
#include "File2.h"
int a;
int main() {
otherFunction();
}
===========================================
File2.cc
===========================================
#include "File1.h"
#include "File2.h"
void otherFunction() {
// do something with a
}
===========================================
Even if you're thinking in terms of "code fragments", you will have a happier time if you express them as functions.
file: whatever1.hpp
#pragma once
inline whatever1(int& arg)
{
// code
}
file: main.cpp
#include <iostream> // say...
#include "whatever1.hpp"
int main() {
int some_variable;
whatever1(some_variable);
// ... etc
}
I know that we use #include<iostream> for cout function.
However, I'm curious when we need to use #include<string>.
Im a student taking C++ class.
Thanks.
Include it whenever you need something from that header. Here you can find a list of things that you need to #include <string> for.
How to wrap this
class Foobar {
public:
int member[];
}
in SWIG, without changing this C++ code??
Here is an excellent post that explains a lot,
SWIG/python array inside structure
but the way it works, you have to change the C++ code to make it possible to wrap. I can't do that.
Well it seems, like I can just go a step further than in SWIG/python array inside structure
I change the above original code foobar.h to :
class Foobar {
public:
int_array_wrapper member;
}
and then, I can both #include and %include this foobar_modified_for_swig.h in my SWIG interface code, at the same time, the original foobar.cpp will still use foobar.h and the two link together and appear to work. Not sure why this works but it seems to work.
As the title suggests I'm experiencing a rather odd problem. When I try to compile a sample source code (that uses libotb) I keep getting errors like the one in the title. What is weird is that #include <iostream> is present in the said source/header where the error is reported.
On the other hand if I extract the code from the said file and create a separate source and compile it with g++ <source_file> it works, but if I compile with g++ -I<path_to_libotb_headers> <source_file> I get the same error, although the source file doesn't include anything from said path.
As stated in the below comments, this issue happens with simply
#include <iostream>
int main
{
std::cerr << "Test";
return 0;
}
#include <ostream>
should fix it. Under C++11, #include <iostream> is supposed to pull in all of <ostream>, but prior to C++11 you had to do the individual #includes.
It should be:
int main ()
you missed the () :)
Verify that your includes all closed their namespaces -- your include may accidentally be declared in a namespace if a previous header did not close its namespaces.
You can also attempt to locate this problem by moving the std includes earlier in the include list.
If you are an Arduino programmer don't forget that Arduino does NOT have any
normal ‘ostream’ stuff build-in.
But there is libraries offering similar functions.
PS. Bear in mind that there is good reasons that streaming type stuff is not included.
Simple question, how do I shorten a call/name without using defines.
For example, I have a singleton that I have to call that is within a namespace (I cannot use using namespace blabla because it is not allowed) like so:
MyFW::GameRoot::Instance()->DoSomething();
Now I can assign that to a variable, which works somewhat if I am using it multiple times within the same class/function, but using it in many classes/functions it becomes cumbersome. I decided to use #define for it:
#define MyFW::GameRoot::Instance() ROOT //defined in GameRoot.h
ROOT->DoSomething(); //Used where-ever GameRoot.h is included
Much better, and I really like it especially because now wherever I see ROOT (color coded through V-Assist) I know what it is immediately... unless I have a breakpoint there and I need Visual Studio to resolve ROOT to show up in the watch window (or even hover over it to quickly pull up the object in debug), which it cannot do.
Is there any other option? What do you guys do to shorten names? Simply use local/member pointers to store the instance?
Thanks!
You can't use using namespace ..., but can you use
namespace root=MyFW::GameRoot;
Then you can type
root::Instance()->DoSomething();
Defining a namespace like that is better than a #define. (I.e it can't get munged up somewhere else by mistake. The compiler knows what you are trying to do.)
Use local references:
MyFW::GameRoot& ROOT = *MyFW::GameRoot::Instance();
Do not use defines.
If you want to ease access across multiple functions, just use a helper function:
namespace {
MyFW::GameRoot* root() { return MyFW::GameRoot::Instance(); }
}
// ...
root()->DoSomething();
Two characters more, but it with comes type-safety included.
The good way to do this (but never in a header) is
using MyFW::GameRoot;
GameRoot::Instance()->DoSomething;
This is a using declaration and is different from a using directive, which is what you mentioned above.