In following code, I think that structure stSameNameButDifferent is local scope definition and so no problem for it. But I got error in run-time.
(error : process crash)
Can you explain what's wrong with that code?
test_function.h
#ifndef TEST_FUNC_H_
#define TEST_FUNC_H_
void test_a();
void test_b();
#endif
main.cpp
#include <iostream>
#include "test_function.h"
using namespace std;
int main(int argc, const char** argv)
{
cout << "testing for struct scope" << endl;
test_a();
test_b();
return 0;
}
test_a.cpp
#include <iostream>
#include <sstream>
#include <cstdint>
#include <list>
#include "test_function.h"
struct stSameNameButDifferent
{
uint32_t nPlayCode;
uint32_t nGameID;
std::string sGameName;
};
void test_a()
{
std::list<stSameNameButDifferent> lstSt;
for(int i=0; i<10; ++i)
{
stSameNameButDifferent st;
st.nPlayCode = i;
st.nGameID = 100+i;
std::ostringstream osBuf;
osBuf << "Game_" << i;
st.sGameName = osBuf.str();
lstSt.push_back(st);
}
for(auto &st : lstSt)
{
std::cout << st.nPlayCode << ", " << st.nGameID << ", " << st.sGameName << std::endl;
}
}
test_b.cpp
#include <iostream>
#include <sstream>
#include <cstdint>
#include <list>
#include "test_function.h"
struct stSameNameButDifferent
{
uint32_t nPlayCode;
uint32_t nGameID;
float fDiscountRate;
std::string sGameName;
};
void test_b()
{
std::list<stSameNameButDifferent> lstSt;
for(int i=0; i<10; ++i)
{
stSameNameButDifferent st;
st.nPlayCode = i;
st.nGameID = 1000+i;
st.fDiscountRate = (float)i/100;
std::ostringstream osBuf;
osBuf << "Game_" << i;
st.sGameName = osBuf.str();
lstSt.push_back(st);
}
for(auto &st : lstSt)
{
std::cout << st.nPlayCode << ", " << st.nGameID << ", " << st.sGameName << std::endl;
}
}
To avoid clashes of the same struct names in multiple translation units, you have to put them in an unnamed namespace like so:
namespace {
struct stSameNameButDifferent {
uint32_t nPlayCode;
uint32_t nGameID;
std::string sGameName;
};
}
This will make stSameNameButDifferent only seen privately in the corresponding translation unit (.cpp file).
Otherwise the linker will resolve the symbol with the first one found, hence the errors you see at runtime.
you have defined stSameNameButDifferent in global scope, so compiler cannot see and analyze both definitions to same struct and it only take the first one it meet and that's why you are getting an error.
You can use two different namespaces for test_a and test_b, so you will not get any error.
Related
Sorry for bad english.
I am new on c++ and trying to understand to send references to class object.
But I get above error. If I use const char then error comes up for 'const' part.
Here is my code:
main:
#include <iostream>
#include "DelMe-ClassHeader.h"
using namespace std;
int main() {
char var1 [2];
int var2;
for (int i = 0; i < 2; i++) {
var1[i] = 2;
}
int var2 = 0;
tc = TestClass(var1, var2);
cout << "before tc.changeValue" << endl;
cout << "var1 is " << var1 << endl;
cout << "var2 is " << var2 << endl;
tc.changeValue()
cout << "before tc.changeValue" << endl;
cout << "var1 is " << var1 << endl;
cout << "var2 is " << var2 << endl;
}
header:
#ifndef TestClass
#define TestClass
#include <iostream>
using namespace std;
class TestClass {
public:
TestClass(char (& first)[2]}, int& second);
void changeValue ();
private:
char (& privArray)[2];
int& privInt;
};
#endif
cpp:
#include "DelMe-ClassHeader.h"
#include <iostream>
using namespace std;
TestClass::TestClass(char (& first)[15], int& second) {
this->priveArray = first;
this->privInt = second;
}
void TestClass::changeValue () {
privInt = atoi(privArray);
}
and the error is:
E:\Programing\CodeBlocks\Cpp\DelMe\DelMe\DelMe-ClassHeader.h|10|error: expected unqualified-id before 'char'
I gratefull for any help
The header contains two errors:
#define TestClass defines TestClass as an empty string. Therefore all occurences of TestClass will be replaced with an empty string, hence the errors. For more information read about the C++ preprocessor.
There is an extra } in the parameter list of TestClass.
Replace with this:
#ifndef TestClass_h_inc_
#define TestClass_h_inc_
#include <iostream>
using namespace std;
class TestClass {
public:
TestClass(char(&first)[2], int& second);
void changeValue();
private:
char(&privArray)[2];
int& privInt;
};
#endif
I have 2 .cpp files called "FactoringProgram.cpp" and "FactoringProgram2nd.cpp", also 1 header file called "FactoringProgram.h". I've already tried searching for this problem on StackOverflow and a couple other sites and haven't found a solution that worked for me. When I try to run this command: "g++ FactoringProgram.cpp FactoringProgram2nd.cpp" I get this error:
FactoringProgram.cpp: In function ‘int main()’: FactoringProgram.cpp:8:11: error: request for member ‘InitialMessage’
in ‘Problem1’, which is of non-class type ‘Factoring()’
Problem1.InitialMessage();
The code for "FactoringProgram.h" is:
#ifndef FactoringProgram_h
#define FactoringProgram_h
#include <stdio.h>
#include <iostream>
class Factoring
{
private:
int m_FirstCoefficent;
char m_FirstOperator;
int m_SecondCoefficent;
char m_SecondOperator;
int m_3rdExpression;
public:
Factoring();
int InitialMessage();
};
#endif
FactoringProgram.cpp code:
#include <stdio.h>
#include <iostream>
#include "FactoringProgramH.h"
int main()
{
Factoring Problem1();
Problem1.InitialMessage();
return 0;
}
FactoringProgram2nd.cpp code:
#include "FactoringProgramH.h"
#include <stdio.h>
#include <iostream>
Factoring::Factoring(int FirstCoefficent=0, char FirstOperator='+',
int SecondCoefficent=1, char SecondOperator='+', int 3rdExpression=1)
: m_FirstCoefficent(FirstCoefficen), m_FirstOperator(FirstOperator),
m_SecondCoefficent(SecondCoefficent), m_SecondOperator(SecondOperator),
m_3rdExpression(3rdExpression);
{
}
int Factoring::InitialMessage()
{
std::cout << "Ok right now your expression is looking like: "
<< FirstCoefficent << "x^2 " << FirstOperator << " " << SecondCoefficent
<< " x" << SecondOperator << " " << 3rdExpression;
}
Your code has several small errors. Here is a version that works (you can compare and see what I had to change). A summary of the changes are:
default parameter values should be in the header file (*.h);
you cannot start variable/argument names with numbers;
int InitialMessage() doesn't return anything, so I changed it to void InitialMessage();
when you initialize fields in a constructor, the last field cannot end with a semi-colon;
fixed the most vexing parsing (per #RSahu comment) in main.cpp.
FactoringProgram.h
#ifndef FactoringProgram_h
#define FactoringProgram_h
#include <stdio.h>
#include <iostream>
class Factoring
{
private:
int m_FirstCoefficent;
char m_FirstOperator;
int m_SecondCoefficent;
char m_SecondOperator;
int m_3rdExpression;
public:
Factoring(int FirstCoefficent = 0, char FirstOperator = '+',
int SecondCoefficent = 1, char SecondOperator = '+', int thirdExpression = 1);
void InitialMessage();
};
#endif
FactoringProgram.cpp
#include "FactoringProgram.h"
Factoring::Factoring(int firstCoefficent, char firstOperator, int SecondCoefficent, char SecondOperator, int thirdExpression) :
m_FirstCoefficent(firstCoefficent),
m_FirstOperator(firstOperator),
m_SecondCoefficent(SecondCoefficent),
m_SecondOperator(SecondOperator),
m_3rdExpression(thirdExpression)
{}
void Factoring::InitialMessage()
{
std::cout << "Ok right now your expression is looking like: "
<< m_FirstCoefficent << "x^2 " << m_FirstOperator << " " << m_SecondCoefficent
<< " x" << m_SecondOperator << " " << m_3rdExpression;
}
main.cpp
#include <stdio.h>
#include <iostream>
#include "FactoringProgram.h"
int main()
{
Factoring Problem1;
Problem1.InitialMessage();
return 0;
}
To this error substitute:
Factoring Problem1();
by:
Factoring Problem1 = Factoring();
The problem is that the compiler is interpreting this line as a function declaration rather than a variable declaration. AS mentioned in the comments, this is known as the most vexing parser problem.
Obs: The code you posted contain many more minor errors.
You should define Factoring() without params and I used headers in FactoringProgram.h
FactoringProgram.h
#include <stdio.h>
#include <iostream>
class Factoring
{
private:
int m_FirstCoefficent;
char m_FirstOperator;
int m_SecondCoefficent;
char m_SecondOperator;
int m_3rdExpression;
public:
Factoring();
Factoring(int,char,int,char,int);
int InitialMessage();
};
#endif
FactoringProgram.cpp
#include "FactoringProgram.h"
int main()
{
Factoring Problem1 = Factoring();
Problem1.InitialMessage();
system("pause");
return 0;
}
FactoringProgram2nd.cpp
#include "FactoringProgram.h"
Factoring::Factoring()
{
*this = Factoring(0, '+', 1, '+', 1);
}
Factoring::Factoring(int FirstCoefficent = 0, char FirstOperator = '+',int SecondCoefficent = 1, char SecondOperator = '+', int _3rdExpression = 1) : m_FirstCoefficent(FirstCoefficent), m_FirstOperator(FirstOperator),m_SecondCoefficent(SecondCoefficent), m_SecondOperator(SecondOperator),m_3rdExpression(_3rdExpression)
{
}
int Factoring::InitialMessage()
{
std::cout << "Ok right now your expression is looking like: "
<< m_FirstCoefficent << "x^2 " << m_FirstOperator << " " << m_SecondCoefficent
<< " x" << m_SecondOperator << " " << m_3rdExpression;
return 0;
}
I have a problem with my program. I made a Graph class in C++, and now I want to sort it topologically. The thing is, my topological sort accepts any DirectedGraph, but when I want to give it a child (AdjacencyListDirectedUnweightedGraph for example), it refuses to convert. Here is my .hpps:
TopoSort.hpp:
#ifndef TOPOSORT_HPP
#define TOPOSORT_HPP
#include "../Graph.hpp"
#include "../DirectedGraph/AdjListUWDG.hpp"
#include "../DirectedGraph/DirectedGraph.hpp"
#include "../UnDirectedGraph/AdjListWUDG.hpp"
class TopoSort
{
protected:
std::vector<int> _sortedList;
std::vector<int> _KahnTopNodes;
public:
TopoSort();
~TopoSort();
void KahnSort(DirectedGraph &list);
void KahnSortTopNodes(DirectedGraph &list);
};
#endif
DirectedGraph.hpp
#ifndef DIRECTEDGRAPH_HPP
#define DIRECTEDGRAPH_HPP
#include <iostream>
#include <string>
#include <vector>
#include "../Graph.hpp"
class DirectedGraph
: public Graph
{
protected:
std::vector<int> _inDegree;
std::vector<int> _outDegree;
public:
DirectedGraph(){};
virtual ~DirectedGraph(){};
int inDegree(int a){return (_inDegree[a]);}
int outDegree(int a){return (_outDegree[a]);}
bool rangeCheck(int a, int b)
{
if (a >= _vertices || b >= _vertices || a == b)
{
std::cout << "The edge " << a << " - " << b << " is invalid." << std::endl;
return (false);
}
return (true);
}
};
#endif
AdjListDG.hpp
#ifndef ADJListDG_HPP
#define ADJListDG_HPP
#include <string>
#include <vector>
#include "DirectedGraph.hpp"
class AdjListDG
: public virtual DirectedGraph
{
protected:
std::vector<std::vector<std::pair<int, int> > > _adjList;
public:
virtual ~AdjListDG();
bool existsEdge(Edge);
bool existsEdge(int, int);
void putEdge(Edge);
void removeEdge(Edge);
int adjacentVertices(int);
bool areAdjacent(int, int);
};
#endif
AdjListUWDG.hpp
#ifndef AdjListUWDG_HPP
#define AdjListUWDG_HPP
#include <string>
#include <vector>
#include "AdjListDG.hpp"
class AdjListUWDG
: public virtual AdjListDG
{
public:
AdjListUWDG(std::string);
virtual ~AdjListUWDG();
};
#endif
And, my main.
#include <iostream>
#include <string>
#include <fstream>
#include "UnDirectedGraph/AdjListWUDG.hpp"
#include "UnDirectedGraph/AdjListUWUDG.hpp"
#include "UnDirectedGraph/AdjMatWUDG.hpp"
#include "UnDirectedGraph/AdjMatUWUDG.hpp"
#include "Assgn3/TopoSort.hpp"
int main(int argc, char** argv)
{
if (argc != 2)
{
std::cout << "Usage : ./graph FILENAME" << std::endl;
return(0);
}
std::string filename = argv[1];
AdjListWUDG gr(filename);
TopoSort tsort;
std::ofstream fichier("results.txt", std::ios::out | std::ios::trunc);
if(fichier)
{
if (gr.existsEdge(1, 2))
fichier << "1 - 2 exist" << std::endl;
fichier << "numedge == " << gr.numEdges() << std::endl;
fichier << "adjver 1 == " << gr.adjacentVertices(1) << std::endl;
fichier << "adj 1 2 == " << gr.areAdjacent(1,2) << std::endl;
fichier << "adj 1 0 == " << gr.areAdjacent(1,0) << std::endl;
fichier << "adj 0 2 == " << gr.areAdjacent(0,2) << std::endl;
}
DirectedGraph * gr2 = &gr;
tsort.KahnSort(*gr2);
}
That's it ! I'm sorry if it looks obvious or something, I just can't see what's the problem. Also tried dynamic and static casts, no success. thanks in advance !
EDIT :
I was stupid. tried to cast for a reference, instead of an object itself...
static_cast instead of static_cast
Sorry for that useless post !
Casting to a reference instead of the object itself, that was stupid, sorry !
I would like to take the data from the struct elements to the internal elements.
What will be a better way to do it.
It shows error: invalid array assignmen berror: invalid array ssignment
error: invalid array assignment error: ‘strcpy’ was not declared in this scope.
#include <iostream>
#include <string>
using namespace std;
struct A
{
char Ip[16];
char port[6];
char sessionkey[32];
}
int main()
{
char m_ip[16];
char m_port[6];
char m_sessionkey[32];
A a;
a.Ip = "10.43.160.94111";
a.port = "12345";
a.sessionkey = "12Abcd12345Abcd12345Abcd1234512";
strcpy(m_ip,a.Ip);
strcpy(m_port,a.port);
strcpy(m_sessionkey,a.sessionkey);
cout << "m_ip:" << m_ip << endl;
cout << "m_port:" << m_port << endl;
cout << "m_sessionkey:" << m_sessionkey << endl;
}
I think you mean the following (C string functions are declared in header <cstring>)
#include <cstring>
//...
char m_ip[16];
char m_port[6];
char m_sessionkey[32];
A a = { "10.43.160.94111", "12345", "12Abcd12345Abcd12345Abcd1234512" };
std::strcpy(m_ip,a.Ip);
std::strcpy(m_port,a.port);
std::strcpy(m_sessionkey,a.sessionkey);
Or instead of
A a = { "10.43.160.94111", "12345", "12Abcd12345Abcd12345Abcd1234512" };
you could write
A a;
a = { "10.43.160.94111", "12345", "12Abcd12345Abcd12345Abcd1234512" };
provided that your compiler supports C++ 2011.
Take into account that you forgot to place a semicol after the closing brace in the structure definition
struct A
{
//...
};
^^^
EDIT: After you unexpectedly changed your code I'd like to point out that this code snippet
A a;
string p = "10.43.160.94111";
string q = "12345";
string r = "12Abcd12345Abcd12345Abcd1234512";
p.copy(a.Ip,16,0);
q.copy(a.port,6,0);
r.copy(a.sessionkey,32,0);
does not make sense. There is no sense to introduce objects of type std::string only that to initialize an object of type struct A.
Another thing you could initially define the structure the following way
struct A
{
std::string Ip;
std::string port;
std::string sessionkey;
};
For writing in C++ prefer to use std::string instead of char * or char[].
A number of your issues will not longer exist if you use std::string instead.
Example:
#include <iostream>
#include <string>
struct A
{
std::string Ip;
std::string port;
std::string sessionkey;
};
int main()
{
std::string m_ip;
std::string m_port;
std::string m_sessionkey;
A a;
a.Ip = "10.43.160.94111";
a.port = "12345";
a.sessionkey = "12Abcd12345Abcd12345Abcd1234512";
// copy data from a to local variables
m_ip = a.Ip;
m_port = a.port;
m_sessionkey = a.sessionkey;
std::cout << "m_ip:" << m_ip << std::endl;
std::cout << "m_port:" << m_port << std::endl;
std::cout << "m_sessionkey:" << m_sessionkey << std::endl;
}
If you insist on using strcpy you must include the C header file string.h either by using #include <string.h> or by using #include <cstring>. Note that this is a C header file and it is distinctly different than the C++ #include <string> header file.
You should change your code like this:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
struct A
{
char* Ip;
char* port;
char* sessionkey;
};
int main()
{
char m_ip[16];
char m_port[6];
char m_sessionkey[32];
A a;
a.Ip = "10.43.160.94111";
a.port = "12345";
a.sessionkey = "12Abcd12345Abcd12345Abcd1234512";
strcpy(m_ip,a.Ip);
strcpy(m_port,a.port);
strcpy(m_sessionkey,a.sessionkey);
cout << "m_ip:" << m_ip << endl;
cout << "m_port:" << m_port << endl;
cout << "m_sessionkey:" << m_sessionkey << endl;
}
strcpy() function is in cstring header file in C++/C++11, so you must add #include<cstring> to your code.
lexical_cast throws an exception in the following case. Is there a way to use lexical_cast and convert the string to integer.
#include <iostream>
#include "boost/lexical_cast.hpp"
#include <string>
int main()
{
std::string src = "124is";
int iNumber = boost::lexical_cast<int>(src);
std::cout << "After conversion " << iNumber << std::endl;
}
I understand, I can use atoi instead of boost::lexical_cast.
If I'm understanding your requirements correctly it seems as though removing the non-numeric elements from the string first before the lexical_cast will solve your problem. The approach I outline here makes use of the isdigit function which will return true if the given char is a digit from 0 to 9.
#include <iostream>
#include "boost/lexical_cast.hpp"
#include <string>
#include <algorithm>
#include <cctype> //for isdigit
struct is_not_digit{
bool operator()(char a) { return !isdigit(a); }
};
int main()
{
std::string src = "124is";
src.erase(std::remove_if(src.begin(),src.end(),is_not_digit()),src.end());
int iNumber = boost::lexical_cast<int>(src);
std::cout << "After conversion " << iNumber << std::endl;
}
The boost/lexical_cast uses stringstream to convert from string to other types,so you must make sure the string can be converted completely! or, it will throw the bad_lexical_cast exception,This is an example:
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>
#define ERROR_LEXICAL_CAST 1
int main()
{
using boost::lexical_cast;
int a = 0;
double b = 0.0;
std::string s = "";
int e = 0;
try
{
// ----- string --> int
a = lexical_cast<int>("123");//good
b = lexical_cast<double>("123.12");//good
// -----double to string good
s = lexical_cast<std::string>("123456.7");
// ----- bad
e = lexical_cast<int>("abc");
}
catch(boost::bad_lexical_cast& e)
{
// bad lexical cast: source type value could not be interpreted as target
std::cout << e.what() << std::endl;
return ERROR_LEXICAL_CAST;
}
std::cout << a << std::endl; // cout:123
std::cout << b << std::endl; //cout:123.12
std::cout << s << std::endl; //cout:123456.7
return 0;
}