STRCPY is undefined c++ [closed] - strcpy

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Im attempting to implement strcpy or strncpy and both are showing an error no matter which i use.
The error is only under strncpy and strcpy
Item.cpp:
#include "Item.h"
#include <iomanip>
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
#include "Date.h"
#include "POS.h"
#include "PosIO.h"
namespace sict {
Item::Item(){
_name = '\0';
_price = 0;
_taxed ='0';
_quantity = '\0';
}
Item::Item(const char* sku, const char * name, double price, bool taxed){
strNcpy(_sku, sku, MAX_SKU_LEN);
name = new char[20];
strcpy(_name, name);
_quantity = 0;
price = _price;
if (price = '\0') {
_taxed = true;
}
}
void Item::sku(const char* value){
strncpy(_sku, value);
}
void Item::price(double p){
p = _price;
}
void Item::name(const char * n){
strcpy(_name, n);
}
}
Any idea how to fix it, ive excluded alot of code from item.cpp thats irrelevant.

Both strcpy and strncpy are declared in the cstring header. You need to include it in order to use the functions:
#include <cstring>

Using std::strcpy();
will solve your issue.

There are two ways to solve this problem, either by including the appropriate header which is #include <cstring> or put std:: in front of the function.
Also, I have noticed that you write strNcpy which can lead to a syntax error.

Related

How to solve this indirect recursion error? [duplicate]

This question already has answers here:
How to have two functions that call each other C++
(2 answers)
Closed 1 year ago.
#include <iostream>
#include <stdio.h>
using namespace std;
void funB(int n){
if (n>1){
cout<<n<<" ";
funA(n/2);
}
}
void funA(int m)
{
if (m>0){
cout<<m<<" ";
funB(m-1);
}
}
int main()
{
funA(20);
return 0;
}
For this code I am getting the following error:
prog.cpp: In function ‘void funB(int)’:
prog.cpp:8:13: error: ‘funA’ was not declared in this scope
funA(n/2);
^
For this simple indirect recursion example, What is the possible error?
I even tried changing the order of the functions.
Try this. What I have done is, I have just declared the funA before the definition of both functions just so that the compiler knows that there exists a function 'funA' which is being referred to. This is called forward declaration.
#include <iostream>
#include <stdio.h>
using namespace std;
void funA(int);
void funB(int n){
if (n>1){
cout<<n<<" ";
funA(n/2);
}
}
void funA(int m)
{
if (m>0){
cout<<m<<" ";
funB(m-1);
}
}
int main()
{
funA(20);
return 0;
}

Add new system call at FreeBSD 10.1

I wanna add new system call at FreeBSD. My system call code is:
#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/mount.h>
#include <sys/sysproto.h>
int Sum(int a, int b);
int
Sum(a,b)
{
int c;
c = a + b;
return (0);
}
But when I rebuild the kernel, I have an error:
What's wrong? Can you help me?
Thanks a lot.
Here's how I did it with my example system call of setkey which takes two unsigned ints.
I added my system call to the end /kern/syscalls.master
546 AUE_NULL STD { int setkey(unsigned int k0, unsigned int k1);}
Then I did
cd /usr/src
sudo make -C /sys/kern/ sysent
Next, I added the file to /sys/conf/files
kern/sys_setkey.c standard
My sys_setkey.c is as follows
#include <sys/sysproto.h>
#include <sys/proc.h>
//required for printf
#include <sys/types.h>
#include <sys/systm.h>
#ifndef _SYS_SYSPROTO_H_
struct setkey_args {
unsigned int k0;
unsigned int k1;
};
#endif
/* ARGSUSED */
int sys_setkey(struct thread *td, struct setkey_args *args)
{
printf("Hello, Kernel!\n");
return 0;
}
Also, I added the system call to /kern/capabilities.conf
##
## Allow associating SHA1 key with user
##
setkey
Finally, while in /usr/src/ I ran the command
sudo make -j8 kernel
sudo reboot
This is a program which runs the system call
#include <sys/syscall.h>
#include <unistd.h>
#include <stdio.h>
int main(){
//syscall takes syscall.master offset,and the system call arguments
printf("out = %d\n",syscall(546,1,1));
return 0;
}
Please read this
I think, that you haven't included your file with sys_Sum function in kernel makefile ( notice, that in your code, that you have provided, function name is Sum and in error there is call to sys_Sum. I hope, that it's just a typo in your code and the name of function is sys_Sum ).

How to translate the following code (for Windows) so that it can be used on a Linux system as well?

The code that I have posted below, is basically used to get (continuous streaming data) from a software and display that data as it is received. The issue that I am facing is, that the software (which already has the "server") is on Windows, but I need to get the data on a separate system running Linux (Ubuntu).
Can anyone guide me regarding what changes do I need to make to the code in-order to make it work on Linux?
Also since they are "communicating" over a network, will there be any changes in the code to point to the server on the windows machine? ( I am sorry for such terminology, I am a bit new to this, so please correct me if I am mistaken)
#include "vrpn_Connection.h" // Missing this file? Get the latest VRPN distro at
#include "vrpn_Tracker.h" // ftp://ftp.cs.unc.edu/pub/packages/GRIP/vrpn
#include "conio.h" // for kbhit()
//== Callback prototype ==--
void VRPN_CALLBACK handle_pos (void *, const vrpn_TRACKERCB t);
//== Main entry point ==--
int main(int argc, char* argv[])
{
vrpn_Connection *connection;
char connectionName[128];
int port = 3883;
sprintf(connectionName,"localhost:%d", port);
connection = vrpn_get_connection_by_name(connectionName);
vrpn_Tracker_Remote *tracker = new vrpn_Tracker_Remote("Tracker", connection);
tracker->register_change_handler(NULL, handle_pos);
while(!kbhit())
{
tracker->mainloop();
connection->mainloop();
Sleep(5);
}
return 0;
}
//== Position/Orientation Callback ==--
void VRPN_CALLBACK handle_pos (void *, const vrpn_TRACKERCB t)
{
printf("Tracker Position:(%.4f,%.4f,%.4f) Orientation:(%.2f,%.2f,%.2f,%.2f)\n",
t.pos[0], t.pos[1], t.pos[2],
t.quat[0], t.quat[1], t.quat[2], t.quat[3]);
}
I would appreciate if anyone could suggest an "easier" alternate to this as well. Thank you!
kbhit() and Sleep() functions are exclusive to Windows.
Here you don't really need kbhit function. You can use an infinite loop instead.
For the sleep method, you can use this code from this thread :
Sleep function in C++
#ifdef _WIN32
#include <windows.h>
void sleep(unsigned milliseconds)
{
Sleep(milliseconds);
}
#else
#include <unistd.h>
void sleep(unsigned milliseconds)
{
usleep(milliseconds * 1000); // takes microseconds
}
#endif
But a much simpler method is to use boost::this_thread::sleep.
This code should work on Linux and Windows.
//...
while(1)
{
tracker->mainloop();
connection->mainloop();
sleep(5000);
}
//...

How to use a QFile with std::iostream?

Is it possible to use a QFile like a std::iostream? I'm quite sure there must be a wrapper out there. The question is where?
I have another libs, which requires a std::istream as input parameter, but in my program i only have a QFile at this point.
I came up with my own solution using the following code:
#include <ios>
#include <QIODevice>
class QStdStreamBuf : public std::streambuf
{
public:
QStdStreamBuf(QIODevice *dev) : std::streambuf(), m_dev(dev)
{
// Initialize get pointer. This should be zero so that underflow is called upon first read.
this->setg(0, 0, 0);
}
protected:
virtual std::streamsize xsgetn(std::streambuf::char_type *str, std::streamsize n)
{
return m_dev->read(str, n);
}
virtual std::streamsize xsputn(const std::streambuf::char_type *str, std::streamsize n)
{
return m_dev->write(str, n);
}
virtual std::streambuf::pos_type seekoff(std::streambuf::off_type off, std::ios_base::seekdir dir, std::ios_base::openmode /*__mode*/)
{
switch(dir)
{
case std::ios_base::beg:
break;
case std::ios_base::end:
off = m_dev->size() - off;
break;
case std::ios_base::cur:
off = m_dev->pos() + off;
break;
}
if(m_dev->seek(off))
return m_dev->pos();
else
return std::streambuf::pos_type(std::streambuf::off_type(-1));
}
virtual std::streambuf::pos_type seekpos(std::streambuf::pos_type off, std::ios_base::openmode /*__mode*/)
{
if(m_dev->seek(off))
return m_dev->pos();
else
return std::streambuf::pos_type(std::streambuf::off_type(-1));
}
virtual std::streambuf::int_type underflow()
{
// Read enough bytes to fill the buffer.
std::streamsize len = sgetn(m_inbuf, sizeof(m_inbuf)/sizeof(m_inbuf[0]));
// Since the input buffer content is now valid (or is new)
// the get pointer should be initialized (or reset).
setg(m_inbuf, m_inbuf, m_inbuf + len);
// If nothing was read, then the end is here.
if(len == 0)
return traits_type::eof();
// Return the first character.
return traits_type::not_eof(m_inbuf[0]);
}
private:
static const std::streamsize BUFFER_SIZE = 1024;
std::streambuf::char_type m_inbuf[BUFFER_SIZE];
QIODevice *m_dev;
};
class QStdIStream : public std::istream
{
public:
QStdIStream(QIODevice *dev) : std::istream(m_buf = new QStdStreamBuf(dev)) {}
virtual ~QStdIStream()
{
rdbuf(0);
delete m_buf;
}
private:
QStdStreamBuf * m_buf;
};
I works fine for reading local files. I haven't tested it for writing files. This code is surely not perfect but it works.
I came up with my own solution (which uses the same idea Stephen Chu suggested)
#include <iostream>
#include <fstream>
#include <cstdio>
#include <QtCore>
using namespace std;
void externalLibFunction(istream & input_stream) {
copy(istream_iterator<string>(input_stream),
istream_iterator<string>(),
ostream_iterator<string>(cout, " "));
}
ifstream QFileToifstream(QFile & file) {
Q_ASSERT(file.isReadable());
return ifstream(::_fdopen(file.handle(), "r"));
}
int main(int argc, char ** argv)
{
QFile file("a file");
file.open(QIODevice::WriteOnly);
file.write(QString("some string").toLatin1());
file.close();
file.open(QIODevice::ReadOnly);
std::ifstream ifs(QFileToifstream(file));
externalLibFunction(ifs);
}
Output:
some string
This code uses std::ifstream move constructor (C++x0 feature) specified in 27.9.1.7 basic_ifstream constructors section of Working Draft, Standard for Programming Language C++:
basic_ifstream(basic_ifstream&& rhs);
Effects: Move constructs from the
rvalue rhs. This is accomplished by
move constructing the base class, and
the contained basic_filebuf. Next
basic_istream::set_rdbuf(&sb) is called to install the contained
basic_filebuf.
See How to return an fstream (C++0x) for discussion on this subject.
If the QFile object you get is not open for read already, you can get filename from it and open an ifstream object.
If it's already open, you can get file handle/descriptor with handle() and go from there. There's no portable way of getting a fstream from platform handle. You will have to find a workaround for your platforms.
Here's a good guide for subclassing std::streambuf to provide a non-seekable read-only std::istream: https://stackoverflow.com/a/14086442/316578
Here is a simple class based on that approach which adapts a QFile into an std::streambuf which can then be wrapped in an std::istream.
#include <iostream>
#include <QFile>
constexpr qint64 ERROR = -1;
constexpr qint64 BUFFER_SIZE = 1024;
class QFileInputStreamBuffer final : public std::streambuf {
private:
QFile &m_file;
QByteArray m_buffer;
public:
explicit QFileInputStreamBuffer(QFile &file)
: m_file(file),
m_buffer(BUFFER_SIZE, Qt::Uninitialized) {
}
virtual int underflow() override {
if (atEndOfBuffer()) {
// try to get more data
const qint64 bytesReadIntoBuffer = m_file.read(m_buffer.data(), BUFFER_SIZE);
if (bytesReadIntoBuffer != ERROR) {
setg(m_buffer.data(), m_buffer.data(), m_buffer.data() + bytesReadIntoBuffer);
}
}
if (atEndOfBuffer()) {
// no more data available
return std::char_traits<char>::eof();
}
else {
return std::char_traits<char>::to_int_type(*gptr());
}
}
private:
bool atEndOfBuffer() const {
return gptr() == egptr();
}
};
If you want to be able to more things like seek, write, etc., then you'd need one of the other more complex solutions here which override more streambuf functions.
If you don't care much for performance you can always read everything from the file and dump it into an std::stringstream and then pass that to your library. (or the otherway, buffer everything to a stringstream and then write to a QFile)
Other than that, it doesn't look like the two can inter-operate. At any rate, Qt to STL inter operations are often a cause for obscure bugs and subtle inconsistencies if the version of STL that Qt was compiled with is different in any way from the version of STL you are using. This can happen for instance if you change the version of Visual Studio.

I can't seem to set default value for multidimensional array in c++

I am trying to set default values for a bool 2d array called "display[Width][Height]" to false. I am getting the error "IntelliSense: braces cannot be omitted for this subobject initializer". The problem is I used this code before and it worked but not anymore. I have these two implementation and header files:
Matrix.cpp (i have indicated where i get the error)
#include "Matrix.h"
Matrix::Matrix():
display{ false } // error is in this line
{
}
Matrix.h
#pragma once
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include <iostream>
class Matrix{
public:
Matrix();
enum { Width = 10, Height = 20 };
private:
bool display[Width][Height];
};
The code works on Linux gcc but not on visual studio. The proper way is to emit the false within the brackets; so display{}

Resources