c++ identifier not found when compiling source - c++

Here is my code:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int userInput = -9999;
userInput = ReadNumber();
WriteAnswer(userInput);
system("pause");
return 0;
};
int ReadNumber ()
{
int liInput = -9999;
cin >> liInput;
return liInput;
};
void WriteAnswer(int data)
{
cout << data << endl;
};
When I try to compile, it saids :
1>error C3861: 'ReadNumber': identifier not found
1>error C3861: 'WriteAnswer': identifier not found
Why did the above error occurs? and how to solve this problem?
Thanks

C++ sources are compiled from beginning to end.
When the compiler has gotten this far:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int userInput = -9999;
userInput = ReadNumber(); // <-- What is this?
It's true -- there is no evidence of ReadNumber existing.
Declare the existence of your functions before they are used.
int ReadNumber ();
void WriteAnswer(int data);

You forgot to type function prototypes.
int ReadNumber ( void );
void WriteAnswer(int );
Put them in your code before calling the functions.

In your code you try to call ReadNumber function, that hasn't been declared yet. The compiler doesn't know anything about this function:
int _tmain(int argc, _TCHAR* argv[])
{
...
ReadNumber(); // call to function ReadNumber, but what is ReadNumber ???
}
// definition of ReadNumber:
int ReadNumber ()
{
...
}
You should declare it first:
// declaration:
int ReadNumber();
int _tmain(int argc, _TCHAR* argv[])
{
...
ReadNumber(); // call ReadNumber that takes no arguments and returns int
}
// definition of ReadNumber:
int ReadNumber ()
{
...
}

You have to write a function prototype or function itself before first call of the function.
In your code compiler see call of the ReadNumber() but it doesn't know what is that function.

Related

How can I write a function that returns to array inside the header?

I am trying to create a function that returns in main.cpp in the header and .cpp file and run it in the main function.
This process I do works on main.
#include <iostream>
#include <sstream>
#include "Cards.h"
using namespace std;
//this function returns array
int *function1(){
int a=12;
int b=13;
int c=14;
static int list[3]={a,b,c};
return list;
}
int main(int argc, const char * argv[]) {
int *list;
list=function1();
cout<<list[1]<<endl;
return 0;
}
However, I cannot do these in a header and a separate cpp file.
I have a Cards header
#ifndef Cards_H
#define Cards_H
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
class Cards{
public:
char suit; //A,H,D,C,S. A is empty card
int number; //00-13
int visibilty;//0 - 1. O invisible 1 is visible
int * function2();
};
#endif
This is the class cpp file
#include "Cards.h"
using namespace std;
//function
int Cards:: function2(){
int a=12;
int b=13;
int c=14;
int list[3]={a,b,c};
return list; // error code Cannot initialize return object of type 'int Cards::*' with an lvalue of type 'int [3]'
}
How do I fix this problem and run it in main?
As pointed out in the comments, there is already a SO thread
Return array in a function
which handles your issue.
If your really want to use C arrays then your program shall look like:
Cards_CStyle.h:
#ifndef Cards_CStyle_H
#define Cards_CStyle_H
using namespace std;
class Cards {
public:
int* function2(int arr[]);
};
#endif
Cards_CStyle.cpp:
#include "Cards_CStyle.h"
using namespace std;
//function
int* Cards::function2(int arr[]){
int a=12, b=13, c=14;
arr[0] = a;
arr[1] = b;
arr[2] = c;
return arr;
}
main_CStyle.cpp:
#include <iostream>
#include "Cards_CStyle.h"
using namespace std;
int main(int argc, const char * argv[]) {
int arr[3]; // Take care that all your functions use size <= 3
Cards cards;
int* list=cards.function2(arr);
cout<<list[1]<<endl;
return 0;
}
As recommended in the comments, you should use the containers of the STL, e.g. array for fixed length or vector for variable length. Assuming that fixed length of 3 will be fine for you, then your code would be looking like this:
Cards_STLStyle.h:
#ifndef Cards_STLStyle_H
#define Cards_STLStyle_H
#include<array>
using namespace std;
typedef array<int, 3> my_array;
class Cards {
public:
my_array function2();
};
#endif
Cards_STLStyle.cpp:
#include "Cards_STLStyle.h"
using namespace std;
//function
my_array Cards::function2(){
int a=12, b=13, c=14;
return my_array { a,b,c};
}
main_STLStyle.cpp:
#include <iostream>
#include <array>
#include "Cards_STLStyle.h"
using namespace std;
int main(int argc, const char * argv[]) {
Cards cards;
my_array list=cards.function2();
cout<<list[1]<<endl;
return 0;
}
Please find more information here:
array

object as key to map becomes const in cpp

can someone please explain why the following code compilation fails with message "passing ‘const apple’ as ‘this’ argument of ‘int apple::foo()’ discards qualifiers", and how to resolve it.
#include <cstdlib>
#include <iostream>
#include <string>
#include <map>
using namespace std;
/*
*
*/
class apple{
private:
int a,b,c,d;
public:
int foo(){
return a+b+c+d;
}
};
class ball{
private:
map<apple,string> mp;
public:
void foo2(){
for(map<apple,string>::iterator it = mp.begin();it!=mp.end();++it){
cout<<it->first.foo()<<endl;
}
}
}
int main(int argc, char** argv) {
return 0;
}
Works for me: (added const at the end of foo() and ; on end of ball class). Class apple is a Key in std::map which is declared as const: typedef pair value_type; so accessing key should be also declared as const.
#include <map>
#include <iostream>
using namespace std;
class apple{
private:
int a,b,c,d;
public:
int foo() const {
return a+b+c+d;
}
};
class ball{
private:
map<apple,string> mp;
public:
void foo2(){
for(map<apple,string>::iterator it = mp.begin();it!=mp.end();++it){
cout<<it->first.foo()<<endl;
}
}
};
int main(int argc, char** argv) {
return 0;
}

Why doesn't the overload function of [] function here?

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <string.h>
class Array
{
public:
int Length;
char *Arrp;
Array(char *str)
{
Length=strlen(str);
Arrp=str;
}
char & operator[](int index);
};
char & Array::operator[](int index)
{
if(index>=Length||index<0)
{
cout<<"Index "<<index<<" error."<<endl;
return Arrp[0];
}
return Arrp[index];
}
int _tmain(int argc, _TCHAR* argv[])
{
Array a("Good");
cout<<a.Arrp[6]<<endl;
return 0;
}
It doesn't get into the overload function of [] at all. I compare it with lots of examples but what I write never functions.
How can I proceed further?
You're not calling the overloaded function but are directly reading the array itself.
Try:
cout<<a[6]<<endl;

visual studio 2012 c++ no cout

I'm trying to learn c++ but a simple method like "cout" and "cin" does not exist
this is my code:
#include "stdafx.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
cout>>"hello";
return 0;
}
there is an error that says "error C2065: 'cout' : undeclared identifier"
and "IntelliSense: identifier "cout" is undefined"
cout is declared in the std namespace:
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "hello";
return 0;
}
You're also doing it wrong. Note how I have the angle brackets aboove.
Add #include <iostream> to stdafx.h. I was having trouble with that and it worked for me.
Adding using namespace std; may help, and also do cout << "hello" not >>.
cout is in std so you to have use using namespace std. And for cout operator is like <<. >> this is for input i.e cin.
#include "stdafx.h";
#include "iostream";
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"hello";
system("pause");
return 0;
}

How to get std::string from command line arguments in win32 application?

So now I have a
int main (int argc, char *argv[]){}
how to make it string based? will int main (int argc, std::string *argv[]) be enough?
You can't change main's signature, so this is your best bet:
#include <string>
#include <vector>
int main(int argc, char* argv[])
{
std::vector<std::string> params(argv, argv+argc);
// ...
return 0;
}
If you want to create a string out of the input parameters passed, you can also
add character pointers to create a string yourself
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string passedValue;
for(int i = 1; i < argc; i++)
passedValue += argv[i];
// ...
return 0;
}
You can't do it that way, as the main function is declared explicitly as it is as an entry point. Note that the CRT knows nothing about STL so would barf anyway. Try:
#include <string>
#include <vector>
int main(int argc, char* argv[])
{
std::vector<std::string> args;
for(int i(0); i < argc; ++i)
args.push_back(argv[i]);
// ...
return(0);
}; // eo main
That would be non-standard because the Standard in 3.6.1 says
An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:
int main() { /* ... */ }
and
int main(int argc, char* argv[]) { /* ... */ }
No. That is not allowed. If present, it is mandated to be char *argv[].
BTW, in C++ main should always be declared to return 'int'
main receives char*. you will have to put the argv array into std::strings yourself.