I am relatively new to C++. I am trying to call a function using defined header. I have following 2 files(in addition to enter.h file):
// 1. main.cpp
#include "enter.h"
#include<iostream>
using namespace std;
int main()
{
int intdemo=enter();
cout << "The result is: " << intdemo<< endl;
}
// 2. enter.cpp
#include <iostream>
using namespace std;
int enter()
{
int thisisanumber;
cout<<"Please enter a number: ";
cin>>thisisanumber;
return thisisanumber;
}
I am getting the following error message "void value not ignored as it ought to be". and is pointed to the second line of the main function where value of the variable "intdemo" is assigned
Can anyone suggest how to fix this error? i have searched some of the similar posts here but cannot able to understand the problem. Since i am a beginner, any help would be appreciated.
Your header file probably declares the function enter() to return void (this was confirmed in the comments).
Changing this to match your function definition will solve the problem, as well as an unresolved external error you will most likely be getting as a side effect of this.
Related
i keep getting this error. i know this is a c++ 11 function but it still isnt working with code blocks c++ compiler. am i using this function correctly of is it a problem with the codeblocks compiler. i tried changing the compiler. using the "have g++ follow the c++11 iso standard" i still keep getting this error. or getting the "stoi() does not exist in the current scope" error
#include <iostream>
#include <string>
using namespace std;
int main()
{
int test = 34;
cout << stoi(test);
}
stoi means "String To Int". It will read an int from a std::string (or std::wstring). See also the reference.
You were probably looking for the reverse std::to_string (reference). But you don't need either, there is no need to convert to string before printing:
#include <iostream>
int main()
{
int test = 34;
std::cout << test;
}
stoi means string to int. So it takes a string as an input.
This should work:
string test = "34"; cout << stoi(test);
#include<bits/stdc++.h>
using namespace std;
int count;
int main()
{
int k;
cin>>k;
count=k;
cout<<count;
return 0;
}
I am trying to change the value of 'count' (Global Variable) in main function but getting reference to 'count' is ambigous error in C++. But the same kind of code works well in C. Please help me.
Remove the using namespace std; line, add the std:: to cin and cout and it should be ok.
You have this compiler error because std::count exist: https://en.cppreference.com/w/cpp/algorithm/count
So it's ambigous for the compiler between std::count and your variable count because you use using namespace std.
This question already has answers here:
Why should I not #include <bits/stdc++.h>?
(9 answers)
Closed 6 years ago.
I was creating a basic tic tac toe game on c++,I got the desired game output without including bits/stdc++ header file,but when included there was an ambiguity for global variable count(which is in use in the below mentioned code). Please explain!
#include <iostream>
#include "unistd.h"
#include <cstdlib>
#include<bits/stdc++.h>
using namespace std;
char a[3][3];
int count=0;
char player_flag ='X';
void init()
{
a[0][0]='1';
a[0][1]='2';
a[0][2]='3';
a[1][0]='4';
a[1][1]='5';
a[1][2]='6';
a[2][0]='7';
a[2][1]='8';
a[2][2]='9';
}
void show()
{
for(int i=0;i<3;i++)
{ for(int j=0;j<3;j++) cout<<a[i][j] << " " ;
cout << "\n" ;
}}
void entry(int n,char player_flag)
{
for(int i=0;i<3;i++)
{ for(int j=0;j<3;j++)
{ if(n==(i*3+j+1))
{if(a[i][j]=='X'||a[i][j]=='O')
{ int n;
cout<<"invalid entry enter another position\n";
cin>>n; entry(n,player_flag);
}
else a[i][j]=player_flag;
}}}}
void turn()
{
if(player_flag=='X') player_flag='O';
else player_flag ='X';
}
void check()
{ int i,j;
for(i=0,j=0;j<3;i=0,j++)
{if(a[i][j]==a[i+1][j]&&a[i+1][j]==a[i+2][j]) {cout<<"\n"<<a[i][j]<<" wins \n"; exit(0);}}
for(i=0,j=0;i<3;j=0,i++)
{if(a[i][j]==a[i][j+1]&&a[i][j+1]==a[i][j+2]) {cout<<"\n"<<a[i][j]<<" wins \n"; exit(0);}}
if(a[0][0]==a[1][1]&&a[1][1]==a[2][2])
{cout<<"\n"<<a[0][0]<<" wins";exit(0);}
else if(a[0][2]==a[1][1]&&a[1][1]==a[2][0])
{cout<<"\n"<<a[0][2]<<" wins";exit(0);}
else if(count>=9){ cout<<"\nits a draw\n"; exit(0);}}
int main()
{ init(); show();
while(1)
{ int n; count++;
cout<<"player "<<player_flag<<" turn: enter position to put \n"; cin>>n;
entry(n,player_flag);
system("clear");
show();
check();
turn();`
}}
error: reference to ‘count’ is ambiguous
else if(count>=9){ cout<<"\nits a draw\n"; exit(0);}}
This is one of many ambiguous count errors.
PS: if bits/stdc++ is not included then its works fine,error pops out only when bits/stdc++ is used. Any reply is encouraged, Thanks!
std::count is a function from the standard library.
http://www.cplusplus.com/reference/algorithm/count/
Since you use the namespace std , "count" can refer to either std::count or the variable count.
You need to either rename your variable, or stop using the std namespace.
You can also include only the c++ headers that you need instead of bits/stdc++.h which includes all of them.
I suspect count is in std namespace somewhere.
Remove the line
using namespace std;
Use the namespace specifier std:: wherever you need it explicitly.
You should not use
#include<bits/stdc++.h>
anyway. Use headers that are part of the standard.
PS
From the answer by #CFrugal:
std::count is a function from the standard library.
http://www.cplusplus.com/reference/algorithm/count/
The files in the bits/ directory are implementation details not to be included in your programs directly. They are included indirectly by the normal includes like <vector> and <iostream>. Since it's an implementation detail, it's allow to make assumptions about the context in which it's included and presumably your include location violates one of those assumptions.
Just include the normal standard header for the functionality you need instead of a bits file.
Upon reading your question a second time it looks like you may also have a second problem: using namespace std bringing the std::count function into the global namespace, which collides with your global int count. To fix this consider using specific functions from standard instead of the entire namespace (using std::cout;), or rename your count variable, or don't declare it at global scope.
Trying to make a simple Calculator but I can't even get my first function to work. I've been trying to be as organized as I can with my headers and .cpps because I remember back in the day those were important. Haha. I'm under the impression the header file is included in the main.cpp and it has the header guards. These are just declarations for my functions correct? And then Calculation'sFunctions.cpp is where I write the code for the previous declaration function I made on my header file. I'm also not sure when to include iostream and stdafx.h and all that etc. Anyway thanks in advance for your help guys, here is my 3 files all I'm trying at this point is to get an integer from the user.
Calculator.cpp
#include "stdafx.h"
#include <iostream>
#include "CalculatorDeclarations.h"
int main()
{
int getFirstInteger(int userInput)
return 0;
}
CalculatorDeclarations.h
#ifndef ADD_H
#define ADD_H
#include "stdafx.h"
#include <iostream>
int getFirstInteger();
#endif
CalculationsFunctions.cpp
#include "stdafx.h"
#include <iostream>
int getFirstInteger(int userInput)
{
std::cout << "Please enter the first integer you would like to use." << std::endl;
std::cin >> userInput;
return userInput;
}
Errors:
Error LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ) ConsoleApplication1 c:\Users\Shane\documents\visual studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\MSVCRTD.lib(exe_main.obj) 1
Error LNK1120 1 unresolved externals ConsoleApplication1 c:\users\shane\documents\visual studio 2015\Projects\ConsoleApplication1\Debug\ConsoleApplication1.exe 1
Well your first problem is your 'main' method
int main()
{
int getFirstInteger(int userInput)
return 0;
}
The syntax is incorrect (should be lined ended with a semi-colon). And you should be passing an int. So it could be modified to this
int main()
{
int j = 1;
getFirstInteger(j)
return 0;
}
But it's worth looking at the implementation here. You're getting the user input within that method, so no need to pass in anything. And you're not using the output, so no need to return anything.
Also you've double included #include<iostream> This won't be a big issue in a small program but it's a waste and bad practice. Only include this where it's needed (which is in CalculationsFunctions.cpp). Don't think you need #include "stdafx.h" at all.
So far I have typed this,
#include iostream
using namespace std;
void PrintIntro();
I want to assign a actual text now to the PrintIntro function so that in my main program I can just type
PrintIntro() and when the program runs the text assign to the function will show.
So far I have tried this after "void PrintIntro();"
{ /*PrintIntro*/
cout <<
"==================================================" << endl;
cout <<
"Welcome to the Math Practice Program!!!!!" << endl;
cout <<
"This Program will help you practice elementary math" << endl;
cout <<
"==================================================" << endl;
/*PrintIntro*/
}
But then I get the error under the "{" symbol indicating that it is "expecting a declaration." I have been searching through notes and messing with this all day and I cannot figure it out. Any help would be appreciated. I am using MS Visual studio Express 2013.
Adding the semicolon after void PrintIntro() tells the compiler that there is a function called PrintIntro that takes no arguments and returns void, and that you are defining it later. This is called a forward declaration. Chances are this is what's happening:
void PrintIntro();
//Compiler: okay, that was a forward declaration
{
//Compiler: wth is this stuff?
}
You want this to happen:
void PrintIntro();
//Compiler: okay, that was a forward declaration
void PrintIntro()
{
//Compiler: oh, this is the definition for that function you told me about earlier
}
Or you want to do it without the forward declaration:
void PrintIntro() //no ';'
{
//Compiler: declaration and function body all in one part - simple!
}
You should also change #include iostream to #include <iostream>
Remove the ; after you declare you function like:
void PrintIntro()
When declaring a function, you do not need to end the function declaration line with a semicolon.
So your function should look like
void PrintIntro(){
blahblahblah...
}
You need to remove the semi column at the end of the declaration
void PrintIntro();
Must be like this
void PrintIntro(){
}
A couple of errors, most of them in the syntax I think. Note that the line #include iostream should actually be #include <iostream> and the ; is missing after void PrintIntro().
Like so
#include <iostream>
using namespace std;
void PrintIntro(){
cout << "Hello world" << endl;
}