I am trying to cout some variables but the compiler says that cout is undefined. I have included iostream and am using namespace std. Removing using namespace std and using std::cout instead changes the issue to "namespace "std" has no member "cout" ". I found some answers saying to add # include "stdafx.h" to the code but Error: cannot open source file "stdafx.h" occurs.
Code is:
#include "Complex.h"
#include <cmath>
#include <iostream>
using namespace std;
Complex::Complex(int PolarOrRectang, float RealOrArg, float ImagOrAng) {
if (PolarOrRectang == 0) {
real = RealOrArg;
imag = ImagOrAng;
else {
real = RealOrArg * cos(ImagOrAng);
imag = RealOrArg * sin(ImagOrAng);
}
};
void Complex::getValue(int PolarOrRectang) {
if (PolarOrRectang == 0) {
cout << real << " +_" << imag << "i" << endl;
} else {
cout << sqrt((real^2) + (imag^2)) << "*e^-" << atan(imag / real)<< endl;
}
};
I'm trying to define a class, so my main is elsewhere.
Running a very basic program that just couts "hello world" works fine, the problem is specific to this code.
Put #include<iostream> at the first position, the order is important
#include "Complex.h"
#include <iostream>
#include <cmath>
PS: Why do you use std:: when you are using "using namespace std;"?
Related
This question already has answers here:
Using scanf/printf to input into/output from a bitset
(3 answers)
Closed 5 months ago.
#include <bitset>
#include <assert.h>
#include <stdio.h>
using namespace std;
int main()
{
bitset<128> bs(42);
bs[11]=0;
bs[12]=1;
assert(bs[12]==1);
printf("bs[11]=%d\n", bs[11]);
printf("bs[12]=%d\n", bs[12]);
return 0;
}
console output:
Why can't I simply get 0 or 1 as output ?
printf with %d is for integer values, whereas std::bitset::operator[] returns a std::bitset::reference.
You can use std::cout from <iostream> header (which is anyway a more c++ "way" to print to the console):
#include <bitset>
#include <assert.h>
#include <iostream>
int main()
{
std::bitset<128> bs(42);
bs[11] = 0;
bs[12] = 1;
assert(bs[12] == 1);
std::cout << "bs[11]=" << bs[11] << std::endl;
std::cout << "bs[12]=" << bs[12] << std::endl;
return 0;
}
Output:
bs[11]=0
bs[12]=1
A side note: better to avoid using namespace std - see here Why is "using namespace std;" considered bad practice?.
With some review comments :
#include <cassert>
#include <bitset>
#include <iostream>
// anything with a .h extension is probably "C" not "C++"
// #include <assert.h>
//#include <stdio.h>
// using namespace std; <== NO, don't use using namespace std;
int main()
{
std::bitset<128> bs(42);
bs[11]=0;
bs[12]=1;
assert(bs[12]==1);
std::cout <<"bs[11]" << bs[11] << "\n";
std::cout << "bs[12]" << bs[11] << "\n";
return 0;
}
If you are using C++ then don't call printf to output something (my compiler refuse to compile your code correctly).
This C++ code works correctly using iostream:
#include <bitset>
#include <iostream>
int main()
{
std::bitset<128> bs(42);
bs[11]=0;
bs[12]=1;
std::cout << "bs[11]=" << bs[11] << std::endl;
std::cout << "bs[12]=" << bs[12] << std::endl;
return 0;
}
I'm learning C++ and tutorial asks me to add another project to what I have now.
Also I'm asked to use forward declaration so I can make use of that added file.
Here is my main project:
#include <iostream>
#include "io.cpp"
using namespace std;
int readNumber();
void writeResult(int x);
int main() {
int x = readNumber();
int y = readNumber();
writeResult(x + y);
return 0;
}
here's the added file called io.cpp:
#include <iostream>
using namespace std;
int readNumber() {
cout << "Enter a number: ";
int x;
cin >> x;
return x;
}
void writeResult(int x) {
cout << "Sum of your numbers is " << x << endl;
}
![And here's a screenshot so you can see what error I'm getting which talks about multiple definition and you can see where those two files are added.
According to the tutorial my code is okay but compiler complains. Why ?]1
In codeblocks, when creating a new class, it should automatically header file. Programming with header files is the best practice out there. Here's the code I tried and it worked, with io.h.
main.cpp
#include <iostream>
#include "io.h"
using namespace std;
io inOut;
int main()
{
int x = inOut.readNumber();
int y = inOut.readNumber();
inOut.writeResult(x + y);
return 0;
}
io.h
#ifndef IO_H
#define IO_H
class io
{
public:
int readNumber();
void writeResult(int);
};
#endif
io.cpp
#include <iostream>
#include "io.h"
using namespace std;
int io::readNumber()
{
cout << "Enter a number: ";
int x;
cin >> x;
return x;
}
void io::writeResult(int x)
{
cout << "Sum of your numbers is " << x << endl;
}
I used codeblocks to compile the code written above, and it worked perfectly.
Well as turns out when adding more cpps they're not supposed to be #included on the top. That's what makes compiler say that function is being defined multiple times. All I had to do was just get rid off that one line.
Here's my source:
http://www.cplusplus.com/forum/beginner/44651/
I am fairly new to c++ (java background) and I'm trying to access a member of a class I have created but I keep getting an error message when trying to call a member of a class, it's saying that the variable is not a member of the class.
Any ideas why this is happening? I've looked at so many other examples of people with this problem, but none of them have helped me find out why
Main.cpp
#include "stdafx.h"
#include "Adressbuch.h"
#include "Kontakt.h"
#include <iostream>
#include <sstream>
using namespace std;
Adressbuch hinzufügen(Adressbuch buch);
Adressbuch löschen(Adressbuch buch);
void auflisten(Adressbuch buch);
int main()
{
bool end = true;
Adressbuch buch;
while (end) {
cout << "Bitte geben sie ein Aktion ein: (hinzufügen(h)/löschen(l)/beenden(b)/auflisten(a))"
<< endl << "zur Zeit gibt es " << buch.adress_buch.size() << " Kontakte" << endl;
if (cin >> "h") buch = hinzufügen(buch);
else if (cin >> "l") buch = löschen(buch);
else if (cin >> "a") auflisten(buch);
else if (cin >> "b") end = true;
else cout << "Error. Ungultig Eingabe." << endl;
}
return 0;
Adressbuch.h
#include "Kontakt.h"
#include <list>
class Adressbuch{
public:
Adressbuch();
~Adressbuch();
void hinzufügen(Kontakt k);
void löschen(Kontakt k);
list<Kontakt> Adressbuch::adress_buch;
};
Adressbuch.cpp
#include "Adressbuch.h"
#include "Kontakt.h"
#include <list>
using namespace std;
Adressbuch::Adressbuch(){
adress_buch;
}
Adressbuch::~Adressbuch(){
}
void Adressbuch::hinzufügen(Kontakt k){
adress_buch.push_back(k);
}
void Adressbuch::löschen(Kontakt k) {
adress_buch.remove(k);
}
The member that I am having trouble with, is the list adress_buch. Anytime I try to call it, it says its not a member, even though it is defined in the header class?
Error message on line 19 of main()
Severity Code Description Project File Line Suppression State
Error C2039 'adress_buch': is not a member of 'Adressbuch'
ConsoleApplication5 c:\users\gregs\documents\visual studio
2015\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp 19
First a minimal, complete verifiable example that contains nothing but the code required to trigger the error:
#include <list>
using namespace std;
class Adressbuch
{
public:
list<int> Adressbuch::adress_buch;
};
int main()
{
Adressbuch buch;
buch.adress_buch.size();
return 0;
}
That's all that's needed, little bit more than, to find the problem. With nothing else in the way as a distraction problem 1 is easy to spot. I'm no guru in the Visual Studio compiler and I don't have one available, but I'm betting that somewhere in the warnings or errors is this line:
list<int> Adressbuch::adress_buch;
adress_buch is improperly defined causing all sorts of future problems. Compiling this example, GCC gives:
error: extra qualification 'Adressbuch::' on member 'adress_buch'
A corrected example is
#include <list>
using namespace std;
class Adressbuch
{
public:
list<int> adress_buch;
};
int main()
{
Adressbuch buch;
buch.adress_buch.size();
return 0;
}
Or better
#include <list>
class Adressbuch
{
public:
std::list<int> adress_buch;
};
int main()
{
Adressbuch buch;
buch.adress_buch.size();
return 0;
}
Because the notorious, bug-hiding using namespace std; has been removed
I am struggling with my first steps in C++. Already asked this question, but did not get the full answer specifically about namespace.
I did the following.
In Visual Studio 2015 created am empty project(New Project -> Visual C++ -> Empty Project)
Then I added two files in Source.cpp and PrintFunc.cpp whose respective contents are as follows.
Source.cpp
#include <iostream>
using namespace std;
int PrintHello();
extern int tempCount;
void main()
{
int i;
PrintHello();
cout << tempCount << endl;
cout << "Hello from main" << endl;
}
PrintFunc.cpp
#include <iostream>
using namespace std;
int tempCount = 111;
int PrintHello()
{
cout << "Hello from Source1" << endl;
return 0;
}
This is compiling perfectly.
Now I am learning about namespaces, so I just tried to put the contents of the second file in a namespace as follows.
PrintFunc.cpp (modified)
#include <iostream>
using namespace std;
namespace MyNameSpace
{
int tempCount = 111;
int PrintHello()
{
cout << "Hello from Source1" << endl;
return 0;
}
}
And now I modified the Source.cpp also to reflect the namespaces introduction in the previous snippets.
#include <iostream>
using namespace std;
int MyNameSpace::PrintHello();
extern int MyNameSpace::tempCount;
void main()
{
int i;
PrintHello();
cout << tempCount << endl;
cout << "Hello from main" << endl;
}
This simply does not compile. Can someone please kindly correct me. My objective is to understand namespace concept in c++. Also I have good exp with C#.
The problem that compiler does not know about namespace MYSpace when compiling Source.cpp.
#include <iostream>
using namespace std;
namespace MyNameSpace
{
int PrintHello();
extern int tempCount;
}
int main()
{
int i;
MyNameSpace::PrintHello();
cout << MyNameSpace::tempCount << endl;
cout << "Hello from main" << endl;
}
But this sample is useless. It work only because you have only one consumer .cpp.
You should use .h file and then include it (PrintFunc.h) in Source.cpp and any other .cpp when you want to use that funtions.
I'll write an example:
Print.h
#pragma once
namespace MyNameSpace
{
int PrintHello();
extern int tempCount;
}
Notice that we dont't use additional includes and using namespace here. We would use includes only to use some classes in functions interfaces.
using namespace could "spoil" consumer's .cpp or .h.
Print.cpp
#include <iostream>
#include "Print.h"
using namespace std;
int MyNameSpace::tempCount = 111;
int MyNameSpace::PrintHello()
{
cout << "Hello from Source1" << endl;
return 0;
}
Here we can set any include it will not touch any other files.
And consumer .cpp:
#include <iostream>
#include "Print.h"
using namespace std;
int main()
{
int i;
MyNameSpace::PrintHello();
cout << MyNameSpace::tempCount << endl;
cout << "Hello from main" << endl;
}
VS specific: #pragma once and for VS you have to #include "stdafx.h" at first line in any .cpp
You have to supply namespace name when using its members. Or use using namespace directive.
void main()
{
int i;
MyNameSpace::PrintHello();
cout << MyNameSpace::tempCount << endl;
cout << "Hello from main" << endl;
}
However, for it to work namespace should be declared in the separate .h file and it should be included in your source.cpp
So finally here it is that I have settled for based on Dmitriy Zapevalov answer.
First Print.h
#pragma once
namespace MyNameSpace
{
int PrintHello();
extern int tempCount;
}
Next PrintFunc.cpp
#include <iostream>
#include "Print.h"
using namespace std;
namespace MyNameSpace
{
int tempCount = 111;
int PrintHello()
{
cout << "Hello from Source1" << endl;
return 0;
}
}
PrintFunc.cpp can also be like this as an alternative.
PrintFunc.cpp (alternative)
#include <iostream>
#include "Print.h"
using namespace std;
int MyNameSpace::tempCount = 111;
int MyNameSpace::PrintHello()
{
cout << "Hello from Source1" << endl;
return 0;
}
And finally Source.cpp
#include <iostream>
#include "Print.h"
using namespace std;
using namespace MyNameSpace;
void main()
{
PrintHello();
cout << tempCount << endl;
cout << "Hello from main" << endl;
}
Source.cpp can also be like this as an alternative.
Source.cpp (alternative)
#include <iostream>
#include "Print.h"
using namespace std;
void main()
{
MyNameSpace::PrintHello();
cout << MyNameSpace::tempCount << endl;
cout << "Hello from main" << endl;
}
I'm working on C++, and this is just a very basic program, but I'm still getting an error.
The error message is:
'class secondary' has no member named 'getting'.
Why is this? It works for my void setting, but not for getting? What am I doing wrong here?
main.cpp
#include <iostream>
#include <string>
#include "secondary.h"
using namespace std;
int main(){
secondary s;
int scale;
cout << "On a scale of 1-10, how awesome are you?" << endl;
cin >> scale;
cout << endl;
s.setting(scale);
cout << s.getting();
return 0;
}
secondary.h
#ifndef SECONDARY_H
#define SECONDARY_H
#include <string>
class secondary
{
public:
void setting(int x);
string getting();
};
#endif // SECONDARY_H
secondary.cpp
#include "secondary.h"
#include <iostream>
#include <string>
using namespace std;
void secondary::setting(int x){
factor = x;
}
string secondary::getting(){
string result;
if(factor < 3){
result = "You have a very low self esteem.";
}elseif(factor > 3){
if(factor > 7){
result = "You have a very high self esteem."
}else{
result = "You have a medium self esteem."
}
}
return result;
}
private factor;
Actually, looking at this again, and deeper, this code has many issues (semicolons missing at key points and the private int definition should have been in the header file, not the cpp file 9t(private is its own section, see below):The problem, from what I can see, s has not yet been instantiated yet, do so and the operation should work correctly.
Please also note that when factor was defined in the cpp file, it was defined at bottom, it should actually be defined before any use of the variable to be defined (in the header file is better meet with common/conventional coding standards).
Please check this tested code:
main.cpp
#include <iostream>
#include <string>
#include "secondary.h"
using namespace std;
int main(){
secondary s;
int scale;
cout << "On a scale of 1-10, how awesome are you?" << endl;
cin >> scale;
cout << endl;
s.setting(scale);
cout << s.getting();
return 0;
}
secondary.h
#ifndef SECONDARY_H
#define SECONDARY_H
#include <string>
class secondary
{
public:
void setting(int x);
std::string getting();
private: // Note: this is how you do private
int factor; // This is the definition with type int, missing in original
};
#endif // SECONDARY_H
secondary.cpp
#include "secondary.h"
#include <iostream>
#include <string>
using namespace std;
void secondary::setting(int x){
factor = x;
}
string secondary::getting(){
string result;
if (factor < 3){
result = "You have a very low self esteem.";
}else if(factor > 3){
if (factor > 7){
result = "You have a very high self esteem.";
}
else{
result = "You have a medium self esteem.";
}
}
return result;
}