cout weird output - c++

When I try using cout, it outputs a random number rather then the sentence I want. There is not compiler error, the program runs fine.
Here is my code:
//question.h
#ifndef _QUESTION_H_
#define _QUESTION_H_
using namespace std;
int first()
{
cout<<"question \n";
return 0;
}
#endif
//main.cpp
#include <iostream>
#include "question.h"
using namespace std;
void main(){
cout<<""<<first<<""<<endl;
cin.ignore();
cin.get();
}
I'm fairly new to writing my own header files, so I'm not sure if I did something wrong with that or if there's a problem with visual studio.

You're printing the address of the function. You need to call it:
cout<<""<<first()<<""<<endl;
^^
As mentioned in the comments, this doesn't have to output what you expect, either. The order in which arguments to functions (and that is just a bunch of function calls) is unspecified, so your function output could be in any position the compiler chooses. To fix this, put separate statements:
cout<<"";
cout<<first(); //evaluated, so output inside first() printed before return value
cout<<""<<endl;
It might not matter with the empty strings, but it will when you replace those with something visible.
Also, don't use void main. Use int main() or int main(int, char**) (see here). Don't use using namespace std;, especially in headers, as std has a lot of crap in it that is pulled in with that statement, leading to easy and confusing conflicts (see here). Finally, choose a name that does not conflict with identifiers reserved for the implementation as your include guard.

You are printing the address of the function first rather than calling it. But changing the function call won't fix your problem all by itself, because first writes to cout internally and then returns a number, which will be printed, which doesn't appear to be what you want.
If you want first to act like an <iomanip> thingie you have to jump through a few more hoops -- read that header to see how it's done.

Use cout<<""<<first()<<""<<endl; you need to actually call the function, not print its address

Related

Undefined reference to a defined method

So I was trying to access a method that is defined in another class and has the prototype in the header. I'm pretty positive I defined it but it keeps popping up undefined reference to SafeCracker.
Main.cpp
#include <iostream>
#include "mystuff.h"
using namespace std;
void BigDog(int KibblesCount);
int main()
{
cout << SafeCracker(1);
return 0;
}
mystuff.cpp
#include <iostream>
using namespace std;
string SafeCracker(int SafeID)
{
return "123456";
}
mystuff.h
using namespace std;
#ifndef MYSTUFF_H_INCLUDED
#define MYSTUFF_H_INCLUDED
string SafeCracker(int SafeID);
#endif // MYSTUFF_H_INCLUDED
Here it tells you that you have an undefined reference, so you don't really have a problem with the prototype.
Had you forgotten to include the header file that contains the prototype you would have gotten something like
main.cpp: In function ‘int main()’:
main.cpp:8:13: error: ‘SafeCracker’ was not declared in this scope
cout << SafeCracker(1);
Your undefined reference is a linker error. The most likely cause would be that you did not use mystuff.cpp when compiling
If you're compiling from the command line, you should give both files as parameters.
If you're using an IDE that calls the compiler, make sure that the file is part of the project.
For example in Code::Blocks right-click on the file name and go "add to project" (If I remember correctly)
It is also possible that you made a typo in the function declaration in mystuff.cpp (that doesn't seem to be the case here though)
Now there is one important thing about your code you should take note of:
It is very bad practice to put a using namespace in a header file.
using namespace std; in a .cpp source file is mostly up to you, and that using statement will only apply to that particular file.
But if you put it in a header file that is meant to be included through #include , the using there will be forced upon any code that includes it.
Here is an example:
main.cpp
#include <iostream>
// including mystuff.h to use that awesome SafeCracker()
#include "mystuff.h"
// I need to use an std::map (basically an associative array)
#include <map>
// the map of my game
class map
{
int tiles[10][10];
};
int main()
{
// The std map I need to use
std::map<int, int> mymappedcontainer;
// The map of my game I need to use
map mytiles;
// The reason why I need to include mystuff.h
cout << SafeCracker(1);
return 0;
}
Normally, my class map should not be a problem since the map I included from the standard library is inside the namespace std, so to use it you would need to go std::map.
The problem here is that, since mystuff.h has using namespace std; in it, the symbol map is already used, and that creates a conflict.
You do not now who will use your header files, or if you will use them again a long time from now, and maybe then you will want to use name that is already used in the std namespace.
I advise you to use std:: before things taken from the standard libraries instead (std::string instead of just string for example)
PS: In C++, "class" refers to a class data structure, and the functions you made here are not part of any class. You should say "defined in another file" or "defined in another translation unit" instead

C++ compilling errors when using Quadprog++ with Eigen together

this is my first question here, I've searched it all over for a long time yet no solution.
I'm using QUadprog++ to solve a quadratic problem. When I use it in a test alone, it was alright. But when I implement it into my project, which contains Eigen, the Eigen operations will have errors like "Matrix A has no member named ‘lu_inverse’". If I comment the header files of Quadprog++ (Array.hh and Quadprog++.hh) out, the errors just disappear. So I assume that it was a conflict error between the header files of Eigen and Quadprog++. Does anyone have some clue? Thanks in advance!
You can also switch to one of QuadProgpp versions which can work with Eigen types directly: https://github.com/asherikov/QuadProgpp, https://www.cs.cmu.edu/~bstephe1/eiquadprog.hpp; or try an alternative implementation of the same algorithm (also Eigen based) https://github.com/asherikov/qpmad.
if your using namespace quadprogpp; then dont. your different libraries have the same typenames and thats causing the errors you have. It may be a few more characters to type quadprogpp::someFunction(); but its worth it. This is also why you shouldn't ever put a using namespace in a header ever. Its because you pollute all files that include that header with the namespace symbols and name conflicts can ensue which is the same kind of error your having right now.
the Quadprog library is in it's own namespace.
#if !defined(_ARRAY_HH)
#define _ARRAY_HH
#include <set>
#include <stdexcept>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
namespace quadprogpp {
enum MType { DIAG };
template <typename T>
class Vector
notice how just after the #includes there is a decleration of namespace quadprogpp{} and everything that is defined in its enclosing brackets will be defined in scope to quadprogpp, so to use any of this library you have to prefix eveything with the namespace name. this is no different than using things from the standard library. I'm quite sure you've written the standard c++ hello world
#include<iostream>
int main()
{
std::cout << "hello world!" << std::endl;
return 0;
}
cout and endl being part of namespace std have to be prefixed with std:: to access them. Many new programmers to c++ dislike this and one of the very first things they google is how to not have to type out namespaces.
#include<iostream>
using namespace std;
int main()
{
cout << "hello world" << endl;
return 0;
}
the next thing new programmers often do is learn to place their definitions in header files and their program logic in cpp files. Thats when they commit the next common mistake.
#ifndef MYHEADER
#define MYHEADER
#include<string>
#include<vector>
#include<iostream>
using namespace std; //Never do this in a header.
doing that pollutes all of your code with everything in the standard library. That may seem like a trivial thing but when you start using another library or perhaps you create your own type that has the same name as things in the standard library that causes name collisions.
That's when the compiler simply cant reason about which Vector you want. But both Quadprog.hh and Array.hh in Quadprog++ are wrapped in namespace quadprogpp to specifically prevent name collision, which is the whole purpose of namespaces. So there is somewhere in your code, likely a header file, where you've made the statement of using namespace quadprogpp;, or some other namespace that defines an Array type, and the compiler can't deduce which type your referring to in your code.
Other than removing your using namespace statements you can also prefix a typename with its namespace qualifer to disambiguate which type your talking about. In your case I'm confident your Array should be declared as quadprogpp::Array arraynamme; rather than simply Array arrayname;

how can i execute a file kept on desktop in c++?

my file structure for executing a .exe is something like this
c:\Documents and settings\Desktop\Release\abc.exe
i want to execute this from other c++ program in vb c++ after building, it generates an error that c:\Document is not external or internal command
few lines of code are as follows:
#include<stdlib.h>
#include<stdio.h>
int main( void ) {
int result;
result=system("c:\\Documents and settings\\Desktop\\Release\\abc.exe");
getchar();
return 0;
}
As I suspected when writing an earlier comment, the way to do it is to wrap the entire string in double-quotes. 'Escaping the spaces' sounds non-sensical to me. 25 seconds of googling and I don't see (nor have I heard of in over 20 years) an escape-sequence for a space character in C.
The solution is indeed to include quotes in the string - not to just wrap the string in a single pair of them, as you've done. The following will do the trick:
#include <stdlib.h>
#include <stdio.h>
int main()
{
int result;
result = system("\"c:\\Documents and settings\\Desktop\\Release\\abc.exe\"");
getchar();
return 0;
}
However, that said - you shouldn't really be using the system call for this job. Since you're on a windows machine, you should use the ShellExecute function instead. There are many reasons for this, which I wont go into here, you can look them up yourself. But suffice to say it's an infinitely better way to invoke another program.
More on ShellExecute: http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx

why does not eclips know cout and cin in my code?

I am new in c++, I want to write my program with eclips but it does not know cout and cin however I add include
This is my code:
class READY {
public:
READY();
virtual ~READY();
#include <iostream.h>
int main (){
cout<<"hello";
}
};
#endif /* READY_H_ */
Move the include and main outside the class and qualify cout with std:::
#include <iostream>
class READY {
public:
READY();
virtual ~READY();
};
int main (){
std::cout<<"hello";
}
C++ is not Java, main resides at the global scope, not as a class member.
Also, it's <iostream>, not <iostream.h>.
Whatever tutorial or book you're following... it isn't any good.
You need to put that #include at the top of your file; including headers in the middle of a class will do weird, weird things! At the very least, it will embed all the names in the header into your class; most likely, it will simply fail to compile.
Furthermore, modern C++ puts cout and essentially every other symbol defined in the standard library into a namespace named std, so you need to write std::cout, or put "using namespace std;" before your class definition, but after the #include.
There are a couple of problems with your code:
The #include statement has to be outside the class declaration. It is good practise to put those at the top of the file and not scatter them through the file as it makes it much easier to check for dependencies in your code by eyeballing the top of the file instead of searching the whole file for #includes.
Your main() function also has to be declared and defined outside the class. In contrast to Jave, main() in C and C++ is a standalone function.
As mentioned, cin and cout live in the std namespace. I would recommend referring to them with the fully qualified name (ie, std::cin and std::cout), although you can use using std::cin; and using std::cout; either inside the function or in your implementation file after all includes
You are including iostream.h - that is the "wrong" file as that is for the old iostreams library. The correct include for the standard compliant iostreams is <iostream>

Scoping with pre-made namespace (C++)

To avoid scoping everything from the STL, you can type
using namespace std;
To avoid scoping only a few things, you can type:
using std::cout;
using std::cin;
I want to write a library that acts the same way. However, instead of being able to include specific classes, I want to be able to include specific collections of functions.
So, for example, I code:
A collection of string functions
A collection of math functions
They are part of the same namespace, but I can include the chunks I want
This is sudo-ish code, but I think it gets my idea across:
namespace Everything{
namespace StringFunctions{
void str1(string & str);
void str2(string & str);
void str3(string & str);
void str4(string & str);
void str5(string & str);
}
namespace MathFunctions {
void math1(int & num);
void math2(int & num);
void math3(int & num);
void math4(int & num);
void math5(int & num);
}
}
then I want to be able to do something like:
#include "Everything.h"
using Everything::Stringfunctions;
int main(){
str1("string"); //this works, I can call this!
math1(111); //compile error: I've never heard of that function!
return 0;
}
Obviously this does not work, and I am kind of confused on how to divide up my library. I don't want to make them classes and then have to use the "dot operator" everywhere, but I also don't want to include a ton of header files.
Maybe I am going about this the wrong way. I hope everyone can help me take the right approach here.
EDIT:
It works by writing:
using namespace Everything::Stringfunctions;
This is very obvious now in hindsight.
The way that you have written your library in the example that you gave is sufficient.
People can get every function from the namespace Everything::Stringfunctions by using the directive using namespace Everything::Stringfunctions.
You should consider splitting functionality into different headers regardless, otherwise you'll wind up with nightmare compilation times. That said I think using namespace Everything::Stringfunctions; ought to do it (with the extra namespace in there. I didn't try compiling though).
What you want seems to work, provided you use using namespace rather than just using. See here (program compiles and outputs '5').