This question already has answers here:
why g++ shows "gets()" not declared ,even after including <cstdio>
(3 answers)
Closed 3 years ago.
With the following code, I get the "gets() was not declared in this scope" error:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
// string str[]={"I am a boy"};
string str[20];`
gets(str);
cout<<*str;
return 0;
}
The function std::gets() was deprecated in C++11 and removed completely from C++14.
As gets() is a C style function, so if you need to include it in your c++ code then you need to include the header file called stdio.h and moreover you can only pass a c style string to gets() function not c++ string class.
So after slight modification in your code it becomes:
#include <iostream>
#include <string.h>
#include "stdio.h"
using namespace std;
int main()
{
// string str[]={"I am a boy"};
char str[20];`
gets(str);
printf("%s",str);
return 0;
}
Related
This question already has answers here:
Conversion from string literal to char* is deprecated [duplicate]
(2 answers)
Closed 2 years ago.
So I'm building a Regex class, with a simple constructor:
regex.hpp
#ifndef REGEX_CUSTOM_CLASS
#define REGEX_CUSTOM_CLASS
#include <stdio.h>
using namespace std;
class Regex
{
private:
/* data */
public:
char *regex;
Regex(char str[]);
};
#endif // REGEX_CUSTOM_CLASS
regex.cpp
#include <iostream>
#include <list>
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include "regex.hpp"
using namespace std;
Regex::Regex(char str[])
{
regex = str;
}
tools.cpp
#include <iostream>
#include <stdio.h>
#include "lib/regex/regex.cpp"
using namespace std;
int main() {
Regex my_regex("//");
cout << my_regex.regex << endl;
return 0;
}
But after I compile it to .exe file and run it, I get this error message:
warning: ISO C++ forbids converting a string constant to 'char*' Regex my_regex("//");
I think the problem is with the data types. What is the problem?
You cannot pass arrays by value. When you write:
Regex::Regex(char str[])
this actually is
Regex::Regex(char* str)
Moreover string literals are of type const char [N] (where N is length of the string including the null terminator) and when passed to functions they decay to const char*. Getting a char* from a const char* (pointer to constant char, not to be confused with constant pointer to char) would break const correctness.
Either use std::string as argument type or change it to const char*.
This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 2 years ago.
I'm working on a project, and inside of the project I've separated my own classes and functions into a directory called base_lib. When compiling the project, I keep getting an undefined reference error for one of the classes in this library. I have been trying to find the answer on many different forums and ultimately just decided to ask it myself. The code:
stack.h
#ifndef __STACK_H
#define __STACK_H
namespace stacker
{
/*******
My code here
*******/
}
#endif
stack.cpp
#include "stack.h"
#include <iostream>
using namespace std;
template <typename var>
stacker::Stack<var>::Stack()
{
head = nullptr;
tail = nullptr;
}
fix.h
#ifndef __FIX_H
#define __FIX_H
#include <iostream>
#include <string>
namespace fixes
{
/*******
My code here
*******/
}
#endif
fix.cpp
#include "fix.h"
#include "base_lib/stack.h"
#include <iostream>
#include <string>
using namespace std;
using namespace stacker;
main.cpp
#include <iostream>
#include "fix.h"
#include "base_lib/functions.h"
using namespace fixes;
using namespace mine;
using namespace std;
The compiler error
C:\...:fix.cpp:(.text+0xa2): undefined reference to `stacker::Stack<char>::Stack()'
...
Does anyone have any tips or tricks?
You seem to have a template in a C++ file instead of a header file.
This means the template will not compile, thus the error you are getting.
Read here: Why can templates only be implemented in the header file?
This question already has answers here:
Why stdfax.h should be the first include on MFC applications? [duplicate]
(3 answers)
Closed 5 years ago.
I expect this is incredibly simple but can't for the life of me figure out whats wrong. I'm new to C++ and I've created a C++ class in visual studio and am trying to use it in the main method of another file. I've stripped everything back to the bare minimum but still I can't get it to run. The first compile error I get is 'Test': undeclared identifier. If I remove 'Test test;' from App.cpp it all compiles fine.
Below is the code. Can anybody help?
App.cpp:
#include "Test.h"
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
int main()
{
Test test;
//cout << test.getNumber() << endl;
return 0;
}
Test.h:
#pragma once
class Test
{
private:
int number;
public:
int getNumber();
Test();
~Test();
};
Test.cpp:
#include "stdafx.h"
#include "Test.h"
int Test::getNumber()
{
return number;
}
Test::Test()
{
this->number = 1;
}
Test::~Test()
{
}
The problem is that "stdafx.h" is pre-compiled.
Visual c++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled.
The solution is to move it to be the first include.
You can read more about it in the wiki page of Precompiled header.
This question already has answers here:
C++ string equivalent for strrchr
(4 answers)
Closed 6 years ago.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main(){
string a="asdasd";
if(!strchr(a,'a')) cout<<"yes";
return 0;
}
I just began to learn C++ programming and I don't know why I got error in this line
if(!strchr(a,'a')) cout<<"yes";
But if I tried to code it like this, it would run very well.
if(!strchr("asdasd",'a')) cout<<"yes";
I know it is a stupid question but I really don't know why.. sorry..
The library function strchr is for use with C-style strings, not the C++ string type.
When using std::string, the closest equivalent of strchr is find:
#include <iostream>
#include <string>
int main(){
std::string a="asdasd";
if(a.find('a') != std::string::npos) std::cout<<"yes";
}
This question already has answers here:
"Y does not name a type" error in C++
(2 answers)
Closed 7 years ago.
I have a ploblem with the next code, I get the error: 'ptab' does not name a type and 'pfreeC' does not name a type, I don't understand how to solve that, thanks for your help =)
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <iomanip>
#include <stdio.h>
#include <Windows.h>
using namespace std;
int *ptab; //Here is the error
ptab=new int[64];
bool *pfreeC; //Here is the error
pfreeC=new bool[11];
The problem is that you have code outside a function body
using namespace std;
int *ptab;
bool *pfreeC;
int main()
{
ptab = new int[64];
pfreeC = new bool[11];
return 0;
}
Of course you should also delete the allocated memory. Or even better, use smart pointers.