SOCKET does not name a type - c++

As the question says, I am having an issue with the following code:
#pragma once
#include "includes.h"
#include "buffer.h"
class CSocket
{
bool udp;
int format;
char formatstr[30];
static sockaddr SenderAddr;
int receivetext(char*buf, int max);
public:
SOCKET sockid;
CSocket(SOCKET sock);
CSocket();
~CSocket();
bool tcpconnect(char*address, int port, int mode);
bool tcplisten(int port, int max, int mode);
CSocket* tcpaccept(int mode);
char* tcpip();
void setnagle(bool enabled);
bool tcpconnected();
int setsync(int mode);
bool udpconnect(int port, int mode);
int sendmessage(char*ip, int port, CBuffer* source);
int receivemessage(int len, CBuffer*destination);
int peekmessage(int size, CBuffer*destination);
int lasterror();
static char* GetIp(char*address);
static int SockExit(void);
static int SockStart(void);
static char* lastinIP(void);
static unsigned short lastinPort(void);
static char* myhost();
int SetFormat(int mode, char* sep);
};
I am using Code::Blocks. I'm getting the following error at build time:
/home/nick/Desktop/39dylibsource/Base Code/39dll/socket.h|15|error: ‘SOCKET’ does not name a type|
/home/nick/Desktop/39dylibsource/Base Code/39dll/socket.h|16|error: expected ‘)’ before ‘sock’|
/home/nick/Desktop/39dylibsource/Base Code/39dll/main.cpp|16|error: expected constructor, destructor, or type conversion before ‘(’ token|
/home/nick/Desktop/39dylibsource/Base Code/39dll/main.cpp|25|error: expected constructor, destructor, or type conversion before ‘(’ token|
/home/nick/Desktop/39dylibsource/Base Code/39dll/main.cpp|34|error: expected constructor, destructor, or type conversion before ‘(’ token|
/home/nick/Desktop/39dylibsource/Base Code/39dll/main.cpp|43|error: expected constructor, destructor, or type conversion before ‘(’ token|
/home/nick/Desktop/39dylibsource/Base Code/39dll/main.cpp|50|error: expected constructor, destructor, or type conversion before ‘(’ token|
/home/nick/Desktop/39dylibsource/Base Code/39dll/main.cpp|59|error: expected constructor, destructor, or type conversion before ‘(’ token|
/home/nick/Desktop/39dylibsource/Base Code/39dll/main.cpp|66|error: expected constructor, destructor, or type conversion before ‘(’ token|
/home/nick/Desktop/39dylibsource/Base Code/39dll/main.cpp|74|error: expected constructor, destructor, or type conversion before ‘(’ token|
/home/nick/Desktop/39dylibsource/Base Code/39dll/main.cpp|85|error: expected constructor, destructor, or type conversion before ‘(’ token|
||=== Build finished: 11 errors, 0 warnings ===|
I have included the following in the referenced includes.h:
#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
Am i missing a directive or library? Is SOCKET spelled different? Any illumination would be great!

The socket file descriptor is just an integer. So replace SOCKET by int in your code, or use a typedef. (I'm not sure where you saw SOCKET before, but it's not standard.)

Related

Weird C++ error, unqualified ID, redeclaration

I want to create a project, I have 3 files, a test.cpp, something.h and a something.cpp. here they are:
test.cpp:
#include <bits/stdc++.h>
#define f cin
#define g cout
#include "something.h"
using namespace std;
int main(void)
{
int x;
register(x);
return 0;
}
something.h:
#ifndef __SOMETHING__H_
#define __SOMETHING__H_
#include <bits/stdc++.h>
void register(int x);
#endif
something.cpp:
#include "something.h"
void register(int x)
{
std::cout << x << '\n';
}
And here is the error I get:
In file included from test.cpp:4:0:
something.h:5:15: error: expected unqualified-id before ‘int’
void register(int x);
^~~
something.h:5:15: error: expected ‘)’ before ‘int’
test.cpp: In function ‘int main()’:
test.cpp:10:15: error: ISO C++ forbids declaration of ‘x’ with no type [-fpermissive]
register(x);
^
test.cpp:10:15: error: redeclaration of ‘int x’
test.cpp:9:9: note: ‘int x’ previously declared here
int x;
^
In file included from something.cpp:1:0:
something.h:5:15: error: expected unqualified-id before ‘int’
void register(int x);
^~~
something.h:5:15: error: expected ‘)’ before ‘int’
something.cpp:3:15: error: expected unqualified-id before ‘int’
void register(int x)
^~~
something.cpp:3:15: error: expected ‘)’ before ‘int’
Why does it tell me that I redefine x? When I just want to call register with it's value.
register is a reserved word in C++. Therefore, you have to give another (unreserved) name.
more information: Register keyword in C++ - Stack Overflow

Compilation error with struct/class defined in a function

What is the problem with the following code. Because if I define the class inside the main function, the compilation fails and I don't understand the compiler error.
Test the code from here
Comment the 1st definition of drift_f (outside of main()) and uncomment the inner definition of drif_t (inside the main() function) and the compiler will get the following error message:
prog.cpp: In function ‘int main()’:
prog.cpp:27:24: error: template argument for ‘template<class> class std::allocator’ uses local type ‘main()::drift_t’
std::deque<drift_t> drift; drift.push_back(drift_t(0,0));drift.push_back(drift_t(-50,-50));
^
prog.cpp:27:24: error: trying to instantiate ‘template<class> class std::allocator’
prog.cpp:27:24: error: template argument 2 is invalid
prog.cpp:27:31: error: invalid type in declaration before ‘;’ token
std::deque<drift_t> drift; drift.push_back(drift_t(0,0));drift.push_back(drift_t(-50,-50));
^
prog.cpp:27:39: error: request for member ‘push_back’ in ‘drift’, which is of non-class type ‘int’
std::deque<drift_t> drift; drift.push_back(drift_t(0,0));drift.push_back(drift_t(-50,-50));
^
prog.cpp:27:69: error: request for member ‘push_back’ in ‘drift’, which is of non-class type ‘int’
std::deque<drift_t> drift; drift.push_back(drift_t(0,0));drift.push_back(drift_t(-50,-50));
#include <iostream>
#include <deque>
using namespace std;
class drift_t{ //It works
public:
int _drift;
int _immediateDrift;
drift_t() : _drift(0), _immediateDrift(0) {}
drift_t(int d, int expected) : _drift(d), _immediateDrift(expected) {}
drift_t(const drift_t& ro) : _drift(ro._drift), _immediateDrift(ro._immediateDrift) {}
drift_t& operator = (const drift_t& ro) { this->_drift = ro._drift; this->_immediateDrift = ro._immediateDrift; return *this; }
} ;//*/
int main() {
/*class drift_t{ //It doesn't works
public:
int _drift;
int _immediateDrift;
drift_t() : _drift(0), _immediateDrift(0) {}
drift_t(int d, int expected) : _drift(d), _immediateDrift(expected) {}
drift_t(const drift_t& ro) : _drift(ro._drift), _immediateDrift(ro._immediateDrift) {}
drift_t& operator = (const drift_t& ro) { this->_drift = ro._drift; this->_immediateDrift = ro._immediateDrift; return *this; }
} ;//*/
std::deque<drift_t> drift; drift.push_back(drift_t(0,0));drift.push_back(drift_t(-50,-50));
return 0;
}
Isn't the error the one that the compiler says it is? You can't use the local class for that template initialization.
Try compiling with -std=c++11 as I believe it relaxes on that.

reading struct -- uint32 does not name a type

#include <iostream>
#include <map>
#include <zlib.h>
#include <vector>
#include <stdint.h>
using namespace std;
int main (int argc, char **argv)
{
if(argc <2){ exit(0);}
//map<int, int> myMap;
struct last_touch
{
vector<uint64> baz;
uint32 foo;
uint32 bar;
}myLastTouch;
gzFile m_fHandle;
m_fHandle = gzopen(argv[1], "rb");
while(!gzeof(m_fHandle))
{
gzread(m_fHandle,&myLastTouch, sizeof(last_touch));
vector<uint64>::size_type sz = myLastTouch.baz.size();
cout<<"size \t"<<sz<<endl;
}
gzclose(m_fHandle);
}
I'm trying to read a struct from a compressed file.
and I compile it using g++ -lz test.cpp
In function ‘int main(int, char**)’:
test.cpp:15: error: ‘uint64’ was not declared in this scope
test.cpp:15: error: template argument 1 is invalid
test.cpp:15: error: template argument 2 is invalid
test.cpp:16: error: ‘uint32’ does not name a type
test.cpp:17: error: ‘uint32’ does not name a type
test.cpp:27: error: ‘uint64’ cannot appear in a constant-expression
test.cpp:27: error: template argument 1 is invalid
test.cpp:27: error: template argument 2 is invalid
test.cpp:27: error: expected initializer before ‘sz’
These are the following errors I get. I think uint32 is because of <stdint.h>and therefore i included it.
Is there somethign else that i'm missing
Those types should be with _t postfix: uint64_t

How to properly use a header file to be a complete class?

(Beginner programmer..) I'm following the style of a header file that worked fine, but I'm trying to figure out how I keep getting all of these errors when I compile. I am compiling with g++ in Cygwin.
Ingredient.h:8:13: error: expected unqualified-id before ‘)’ token
Ingredient.h:9:25: error: expected ‘)’ before ‘n’
Ingredient.h:19:15: error: declaration of ‘std::string <anonymous class>::name’
Ingredient.h:12:14: error: conflicts with previous declaration ‘std::string<anonymous class>::name()’
Ingredient.h:20:7: error: declaration of ‘int <anonymous class>::quantity’
Ingredient.h:13:6: error: conflicts with previous declaration ‘int<anonymous class>::quantity()’
Ingredient.h: In member function ‘std::string<anonymous class>::name()’:
Ingredient.h:12:30: error: conversion from ‘<unresolved overloaded function type>’ to non-scalar type ‘std::string’ requested
Ingredient.h: In member function ‘int<anonymous class>::quantity()’:
Ingredient.h:13:25: error: argument of type ‘int (<anonymous class>::)()’ does not match ‘int’
Ingredient.h: At global scope:
Ingredient.h:4:18: error: an anonymous struct cannot have function members
Ingredient.h:21:2: error: abstract declarator ‘<anonymous class>’ used as declaration
And here is my class header file...
#ifndef Ingredient
#define Ingredient
class Ingredient {
public:
// constructor
Ingredient() : name(""), quantity(0) {}
Ingredient(std::string n, int q) : name(n), quantity(q) {}
// accessors
std::string name() { return name; }
int quantity() {return quantity; }
// modifier
private:
// representation
std::string name;
int quantity;
};
#endif
I am confused by these errors and don't really know what I am doing wrong concerning the implementation of the class..
That's a funny one. You are essentially killing your class name by #define Ingredient - all occurrences of Ingredient will be erased. This is why include guards generally take the form of #define INGREDIENT_H.
You are also using name both for the member and the getter function (probably an attempt to translate C#?). This is not allowed in C++.
How about look on errors? variables and functions can't have same names. And include guard should never names such as class.
#ifndef INGREDIENT_H
#define INGREDIENT_H
class Ingredient {
public:
// constructor
Ingredient() : name(""), quantity(0) {}
Ingredient(std::string n, int q) : name(n), quantity(q) {}
// accessors
std::string get_name() const { return name; }
int get_quantity() const {return quantity; }
// modifier
private:
// representation
std::string name;
int quantity;
};
#endif

"invalid conversion from" with pthread_create issue

Any comment is appreciated for the compile error below.
Although my question is similar to other thread: pthread function from a class, I still haven't been able to solve my problem. I am still not that familliar with pointer, and thread programming in C & C++.
Error
../src/Main.cpp: In function ‘int main(int, char**)’:
../src/Main.cpp:22: error: invalid conversion from ‘unsigned int* (*)(void*)’ to ‘void* (*)(void*)’
../src/Main.cpp:22: error: initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’
make: *** [src/Main.o] Error 1
Main.cpp
#include <process.h>
#include "ThreadInstance.hpp"
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char** argv)
{
pthread_mutex_t mutex;
int ht1;
pthread_t threadId1;
pthread_attr_t attr1;
pthread_mutex_init(&mutex, NULL);
pthread_attr_init(&attr1);
pthread_attr_setdetachstate(&attr1, PTHREAD_CREATE_DETACHED);
ht1 = pthread_create(&threadId1,
&attr1,
&ThreadInstance::ThreadEntryPoint,
//(void *)readThread);
NULL);
unsigned long rc = 0;
rc = pthread_join(ht1, NULL);
return 0;
}
ThreadInstance.hpp
#ifndef _SCENE_CLASSIFY_THREAD_H
#define _SCENE_CLASSIFY_THREAD_H
#ifndef STDCALL
#define STDCALL __attribute__((stdcall))
#endif
using namespace std;
class ThreadInstance
{
public:
ThreadInstance();
ThreadInstance(int camNum);
void startUp();
static unsigned STDCALL* ThreadEntryPoint(void* pThis)
{
//static unsigned __stdcall ThreadEntryPoint(void* pThis) {
ThreadInstance *ptr = (ThreadInstance*) pThis;
ptr->startUp();
//return 1; // Returns error "invalid conversion from ‘int’ to ‘unsigned int*’" when the function is declared as pointer.
// Since returning either 1 or 0,
// compile error still occurs.
// So this return value should not be the cause.
return 0;
}
~ThreadInstance();
};
#endif
Note: Only necessary part is shown
Your ThreadEntryPoint must return void*.
The error indicates the type that is expected, and that is the function pointer type that you are required to use.
The start function returns void* and takes void*. Yours returns unsigned int*.
Change the method to return a pointer to void.