What's wrong I'm doing? Strange, because signature of constructor is the same. The compiler says:
'ShaderProgram::ShaderProgram(std::vector< int*, std::allocator< _Ty >>)': overloaded member function not found in 'ShaderProgram'.
This error occures, when I use 3 files with code(below), but when I put this code in 1 file(main cpp) - it works
//main.cpp
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include "ShaderHelpers.h"
int main(int argc, const char* argv[]) {
int* a = new int(5);
int* b = new int(7);
ShaderProgram *sp = new ShaderProgram(std::vector<int*>{ a, b});
return 0;
}
================================================================
//shader.cpp
#include "ShaderHelpers.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <string>
ShaderProgram::ShaderProgram() { }
ShaderProgram::ShaderProgram(std::vector<int*> shaders)
{
Shaders = shaders;
for each (int* i in shaders)
{
std::cout << i;
}
}
ShaderProgram::~ShaderProgram()
{
std::cout << "delete";
}
===============================================================
//ShaderHelper.h
#pragma once
#include <string>
class ShaderProgram
{
public:
std::vector<int*> Shaders;
ShaderProgram(std::vector<int*> shaders);
~ShaderProgram();
private:
ShaderProgram();
};
I just needed to add
# include <vector>
into the ShaderHelper.h
Related
I am making something in c++, it doesn't have any errors visible in Visual Studio code, but when I use g++ to be able to execute it, I get this error:
In file included from Main.cpp:6: In file included from ./Filechange/Filechange.hpp:1: ./Filechange/Filechange.cpp:14:24: error: expected expression
std::thread first ([&wtime,&f,&fn]() mutable {
^ Main.cpp:16:33: error: expected expression
OnFilechange("FileEvent", 0.5, [](char* txt){
^ 2 errors generated.
These are the files:
Main.cpp:
#include <lua.hpp>
#include <iostream>
#include <chrono>
#include <thread>
#include <stdio.h>
#include "Filechange/Filechange.hpp"
void wait(int seconds)
{
std::this_thread::sleep_for(std::chrono::seconds(seconds));
}
int main(int argc, char const *argv[])
{
lua_State *State = luaL_newstate();
OnFilechange("FileEvent", 0.5, [](char* txt){
std::cout << txt << std::endl;
});
lua_close(State);
return 0;
}
Filechange.cpp:
#include <thread>
#include <stdio.h>
#include <stdlib.h>
#include <chrono>
#include <string>
#include <fstream>
char* StringToChar(std::string str){
char* Array = new char[str.length() + 1];
strcpy(Array,str.c_str());
return Array;
}
void OnFilechange(const char *f, float wtime, void (*fn)(char* txt)){
std::thread first ([&wtime,&f,&fn]() mutable {
std::ifstream file(f);
std::string str;
std::string filecontents;
while (std::getline(file,str)){
filecontents += str;
filecontents.push_back('\n');
}
char* LastContents = StringToChar(filecontents);
char* CurrentContents = StringToChar(filecontents);
while (true){
if (wtime != 0){
std::this_thread::sleep_for(std::chrono::milliseconds(int(wtime*1000)));
}
filecontents = "";
while (std::getline(file,str)){
filecontents += str;
filecontents.push_back('\n');
}
CurrentContents = StringToChar(filecontents);
if (strcmp(LastContents, CurrentContents) != 0){
LastContents = StringToChar(filecontents);
fn(StringToChar(filecontents));
}
}
});
}
Filechange.hpp:
#include "Filechange.cpp"
#ifndef FILECHANGE_HPP
#define FILECHANGE_HPP
#include <thread>
#include <stdio.h>
#include <stdlib.h>
#include <chrono>
#include <string>
#include <fstream>
void OnFilechange(const char *f,float wtime,void (*fn)(char txt));
#endif
There's also a extension less file named FileEvent which will change in the runtime using other code files.
The Filechange.cpp and Filechange.hpp are in a folder named "Filechange"
This function:
void OnFilechange(const char *f, float wtime, void (*fn)(char* txt))
expects a function pointer, and a lambda in g++ is not implemented as a function pointer. Instead, you should declare the function to take a std::function, as in:
void OnFilechange(const char *f, float wtime, std::function<void(char *)> fn)
You may also need #include <functional> to get the declaration of std::function.
use -std=c++17 in g++ if possible as g++ defaulted to c++98
I have the following issue thrown by the compiler:
include/FlowChannel.h:14:21: error: ‘LatticeCell’ was not declared in this scope
std::vector grid;
when having these 3 header files (LatticeCell.h, FlowChannel.h and Utilities.h) and 2 cpp files including them(lbm.cpp and Utilities.cpp):
LatticeCell.h
#ifndef LATTICECELL_H
#define LATTICECELL_H
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
/* Single cell */
using namespace std;
class LatticeCell{
private:
std::vector<double> matrix = {0,0,0,0,0,0,0,0,0};
unsigned int type; //fluid, no-slip, velocity or density
public:
//Constructor
LatticeCell(unsigned int inType){
type = inType;
}
};
#endif
FlowChannel.h
#ifndef FLOWCHANNEL_H
#define FLOWCHANNEL_H
#include <vector>
#include <string>
#include <cmath>
#include <iostream>
#include "LatticeCell.h"
using namespace std;
class FlowChannel{
private:
std::vector<LatticeCell> grid; //ERROR LINE
unsigned int dimX = -1;
unsigned int dimY = -1;
public:
FlowChannel(unsigned int nx, unsigned int ny){
dimX = nx+2;
dimY = ny+2;
unsigned int gridSize = dimX*dimY;
grid.reserve(gridSize);
initGrid(/*TODO Params*/);
}
};
#endif
lbm.cpp
#include <string>
#include <vector>
#include "LatticeCell.h"
#include "FlowChannel.h"
#include "Utilities.h"
int main(int argc, char** argv){
printsomething();
return 0;
}
Utilities.cpp
#include "LatticeCell.h"
#include "FlowChannel.h"
#include <string>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
void printsomething(){
cout << "something" << std::endl;
}
double calcRelaxationTime(unsigned int ny , double reynolds, double uin){
return 3.0 * (uin * ny / reynolds) - 0.5;
}
Utilities.h
#ifndef UTILITIES_H
#define UTILITIES_H
#include "LatticeCell.h"
#include "FlowChannel.h"
#include <vector>
#include <cmath>
void printsomething();
#endif
Further my compiler flags are:
-Wall -std=c++17 -pedantic
For some reason I can't figure out, why LatticeCell wouldnt be a declared class in FlowChannel, due to it being included. Do you guys know whats wrong?
Edit: I added lbm.cpp, Utilities.cpp and Utilities.h so you guys see the full scope of the problem
You should check if the files are in the same directory.
I copy and paste your code in VS 2019 and it work for me,
here are the pictures
FlowChannel LatticeCell
It seems, that deleting #include 'LatticeCell.h' everywhere but in FlowChannel.h. I dont get the error 100% to be honest, as this wouldn't execatly cause an include loop that would induce such an error, but it works.
SIMPLY PUT Why does my text data file myData.cpp get the error Expected unqualified-id before '{' token? The file alone gives rise to this error and has been reproduced here http://coliru.stacked-crooked.com/a/7f32b5e643fb4d52
// ***** myData.cpp ******
{ // <---- error occurs here
{ "*****", "Error" },
{ "00-01", "Instructional exposition (textbooks, tutorial papers, etc.)" },
{ "00-02", "Research exposition (monographs, survey articles)" },
{ "00A05", "General mathematics" }
}
MORE DETAIL. Potentially helpful, but not necessary to reproduce error.
Right now, with 2 files main.cpp and myFunctions.cpp, everything works. But when I split it into 3 files main.cpp, myFunctions.cpp, and myData.cpp, I get the error Expected unqualified-id before '{' token.
I want to make it 3 files, because the text data for myData.cpp is pretty long and I don't want it to clutter myFunctions.cpp.
This is what I have as 2 files that compiles.
// ***** main.cpp *****
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <string>
#include <iostream>
using namespace std;
extern size_t msc_get_no(const char*);
int main(int argc, char** argv)
{
assert(argc >= 0);
return (int)msc_get_no(argv[1]);
}
// ****** myFunctions.cpp *****
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>
extern size_t msc_get_no(const char*);
struct msc_data
{
const char* code;
const char* desc;
};
typedef struct msc_data MSCDat;
static const MSCDat mscdat[] =
{
{ "*****", "Error" },
{ "00-01", "Instructional exposition (textbooks, tutorial papers, etc.)" },
{ "00-02", "Research exposition (monographs, survey articles)" },
{ "00A05", "General mathematics" }
}
;
static const size_t msccnt = sizeof(mscdat) / sizeof(mscdat[0]);
static int msc_cmp(const void* a, const void* b)
{
const char* msc_code = static_cast<const char*>(a);
const MSCDat* p = static_cast<const MSCDat*>(b);
return strcmp(msc_code, p->code);
}
size_t msc_get_no(const char* msc_code)
{
MSCDat* p;
p = (MSCDat*) bsearch(msc_code, &mscdat[0], msccnt, sizeof(mscdat[0]), msc_cmp);
return p - &mscdat[0];
}
This is what I have as 3 files that does not compile, because of the Expected unqualified-id error in myData.cpp. The only difference is with myFunctions.cpp, so I have excluded main.cpp and myData.cpp.
// ***** myFunctions.cpp *****
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>
extern size_t msc_get_no(const char*);
struct msc_data
{
const char* code;
const char* desc;
};
typedef struct msc_data MSCDat;
static const MSCDat mscdat[] =
#include "myData.cpp" //<------ only here is different
;
static const size_t msccnt = sizeof(mscdat) / sizeof(mscdat[0]);
static int msc_cmp(const void* a, const void* b)
{
const char* msc_code = static_cast<const char*>(a);
const MSCDat* p = static_cast<const MSCDat*>(b);
return strcmp(msc_code, p->code);
}
size_t msc_get_no(const char* msc_code)
{
MSCDat* p;
p = (MSCDat*) bsearch(msc_code, &mscdat[0], msccnt, sizeof(mscdat[0]), msc_cmp);
return p - &mscdat[0];
}
THANK YOU
Your IDE tries to compile myData.cpp by itself. But this file is only an include file. If you rename it to myData.h (or even
myData.dat) everthing should be fine.
What I'd like to do, is add an object to my obiektGeometryczny vector, which would be a Manipulator or kwadrat type.
I want "przeszkoda" to be an obstacle of Manipulator or kwadrat (square in polish) type.
I've tried to use:
obiektGeometryczny.push_back(new Manipulator());
but it returns:
src/scena.cpp:71:36: error: expected type-specifier before ‘*’ token
obiektGeometryczny.push_back(new *Manipulator);
Below is the code:
scena.hh
#ifndef SCENA_HH
#define SCENA_HH
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <unistd.h>
#include <vector>
#include "manipulator.hh"
#include "kwadrat.hh"
#include "przeszkoda.h"
class scena{
vector<przeszkoda*> obiektGeometryczny;
public:
scena(int argc, char *argv[]);
};
#endif
przeszkoda.hh
#ifndef PRZESZKODA_HH
#define PRZESZKODA_HH
class przeszkoda{
virtual void czyPrzeciecie() {;};
};
#endif
manipulator.hh
#ifndef MANIPULATOR_HH
#define MANIPULATOR_HH
#include "przeszkoda.hh"
class Manipulator : public przeszkoda
{
void czyPrzeciecie();
};
#endif
kwadrat.hh
#ifndef KWADRAT_HH
#define KWADRAT_HH
#include "przeszkoda.hh"
class kwadrat : public przeszkoda
{
void czyPrzeciecie();
};
#endif
It is not minimal example - there is something more you didn't show us. Code you posted is ok - try to simplify your case, because something like this works correctly:
#include <vector>
using namespace std;
class A {
int x;
};
int main(void) {
vector<A*> v;
v.push_back(new A());
return 0;
}
Im new in C++, and i got a program called sendSMS:
#include "ServerSocket.h"
#include "SocketException.h"
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include "Socket.h"
#include <pthread.h>
#include <stdio.h>
#include "Config.h"
#include <mysql/mysql.h>
#include <cstdlib>
#include <SerialStream.h>
void *func_servidor(void *ptr_timer);
ServerSocket server(30001);
pthread_t thread_servidor;
pthread_t thread_transfdados;
pthread_t thread_BD;
pthread_cond_t cv;
pthread_mutex_t mp;
int ret;
#define CTRL_C "\x1A"
const int PORT_MON = 30000;
const string serialPort = "/dev/ttyS0";
using namespace LibSerial;
using namespace std;
int setSerial(SerialStream& ssStream, const string& port) {
...
....
}
int sendsms(int argc, char **argv) {
bool send = true;
...
....
return(0);
}
void *func_servidor(void *ptr)
{
...
....
return(0);
}
I want to transfer all those functions to a header .h file and call it in a main program(main.cpp), so the main.cpp only calls "everything" like:
#include sendSMS.h
class modem{
{
public:
void SendSms();
}
int main{
SendSms();
return(0);
}
And the header will be like?
#ifndef __SENDSMS__
#define __SENDSMS__
#include "ServerSocket.h"
#include "SocketException.h"
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include "Socket.h"
#include <pthread.h>
#include <stdio.h>
#include "Config.h"
#include <mysql/mysql.h>
#include <cstdlib>
#include <SerialStream.h>
class sendsms
{
private:
int ret;
const int PORT_MON;
int argc;
const string serialPort;
char **argv;
bool send;
public:
sendsms();
void *func_servidor(void *ptr);
int setSerial(SerialStream& ssStream, const string& port);
int sendsms();
};
#endif
In your main.cpp you will need an object of type modem in order to call sendsms() from main():
#include sendSMS.h
class modem{
{
public:
void SendSms();
}
int main(int argc, char **argv) {
modem m;
m.SendSms();
return(0);
}