I am trying out a technique for a singleton class:
// access controlled singleton, accessed through function "instance()" // singleton is constructed in this function // so that constructor and destructor will be used class single { // private constructor/destructor
[Code] .....
Playing around with the code in main(), I am having trouble with auto:
single& s = single::instance(); // works fine auto a = single::instance(); // error ~single() is private
When I make the destructor public, the output of the program is:
ctor dtor dtor
So I fixed this by typing auto&. I'm still confused though, why wouldn't auto know I am returning a reference?
I am writing a class Player which has several char arrays as private fields. I am trying to write a method which returns an array as a pointer, but doesn't alter the array in any way, thus the const.
Here is a snippet:
Code: class Player { private: char state[MAX_STATE_CHAR + ONE_VALUE]; int rating; char last[MAX_NAME_CHAR + ONE_VALUE]; char first[MAX_NAME_CHAR + ONE_VALUE]; int groupNumber = NEG_ONE; public: char * GetFirst() const { return first; }
Visual studio is saying that the return type doesn't match.
Here is my code to find the index of a string array whose string is equal to the query string. I have checked the program can return the correct index, but the cout result is totally wrong.
#include <iostream> #include <string> using namespace std;
The idea of the code is to make a console to parse some commands. And have some commands with arguments and others no. Without the static the program return other error that is...
Is this even syntactically correct? It gives me errors. Im just trying to compile it without errors. I think the function makes sense since its returning a type Class
I am new to c++ and trying to learn. for instance. i have a struct and method.I am trying to learn what i can do with the method if i define the return type as struct type.
struct S { int age; string name; }; S method() { //what i can do in here. with the Struct. I mean can i reach members of the struct. etc }
I am wondering why return type for an assignment operator cant be a void or int? Cant I write assignment operator for student class like this as we do nothing with returned value?
Student { char name[20]; int marks; public: student(char*name,int marks)
I am having problems with my function definition of a function that should return a structure value.
This is the error I get compute.cpp(9): error C2146: syntax error : missing ';' before identifier 's_advertisebus'
The error is on the line where I start my function definition typing my function type as a structure. A long time ago in c the keyword struct is used with the structure type like struct s_advertisebus s_readadbus(). I tried it both ways but I got errors.
// struct.h #ifndef STRUCT_H #define STRUCT_H
struct s_advertisebus { int nnumberofads; float fpercentused;
If we are using strcpy() for copying the string. As we are passing pointers to it It will copy the string & no need to return the string .This function will finely work with return type as void then why Ritchie has used it as char* strcpy()?
test.cpp: In function ‘int main()’: test.cpp:20:30: error: no matching function for call to ‘func1(std::vector<int>&)’ test.cpp:20:30: note: candidate is: test.cpp:8:45: note: template<class T, class U> std::map<T, T> func1(U) test.cpp:8:45: note: template argument deduction/substitution failed: test.cpp:20:30: note: couldn't deduce template parameter ‘T’
I am trying to create a flexible interface for my CL application. And what i have is this :
Code: using namespace std; // iplcp -i queryFile -d databaseFile template <typename INT, typename CHARA> class API {
vector<string> files; vector<INT> flags;
[Code] ....
and in main :
int main(int argc, char **argv){ //set variables API<int, char**>args(argc,argv); cout << "In file: "<< args.GetOpt("i") << " Db file: " << args.GetOpt("h") << endl; }
// first thing to be printed should be string and the second int
I know this is not probably the best way to but i am laying around and was curious if something like this could work . Are there any good C++ templates for CLI applications from which i could learn?
I'm trying to write a simple Delegate class with a Bind() and Invoke() function. For now it only needs to support a void class function with no parameters. I've searched around and found quite a few exmaples, though, those class are heavily templated and I lose track trying to simplify it.
So far my code is following:
Code: #include <windows.h> class Test { public: void DoSomething() { MessageBox(NULL, L"Test::DoSomething!", NULL, 0);
[Code] ....
The part I am having difficulty with is assigning &Test::DoSomething to the m_Callback variable.
&tObject::DoSomething works, yet _Callback which I pass &Test::DoSomething to does not work.
I'm writing some functions pertaining to binary trees. I've used recursion once before while learning quicksort but am still quite new and unfamiliar with it. And this is my first time touching a binary tree. So my question: In my addnode function, will the return root statement at the end ever return a value other than the value passed to the function?
#include <iostream> using namespace std; int n; int& test();
[Code] ....
Explanation
In program above, the return type of function test() is int&. Hence this function returns by reference. The return statement is return n; but unlike return by value. This statement doesn't return value of n, instead it returns variable n itself.
Then the variable n is assigned to the left side of code test() = 5; and value of n is displayed.
I don't quite understand the bold sentence. Shouldn't value of n and variable n be the same?
Something I am noticing is that temp right after the assignment to *bar, is not the same value as *bar. This is for a project at work and the code runs on an embedded board with an ARM processor. I've copied the function into a standalone program for both Visual Studio and Code::Blocks and it works correctly there.