Debugger doesn't hit breakpoint - c++

simple code, in main (bottom of this post) there is literally one line of code, yet VS2008 marks this line as unreachable!!!
Could someone please take a look and tell me what's going on with it.
One note, if I delete the line marked as //DELETED TO WORK in class Reader, this code miraculously works. Tried many things, pointing debugger to source code etc, etc. None of the above works.
Help needed.
Thank you.
//AClass.hpp
class AClass
{
int x ;
public:
AClass(void);
~AClass(void);
};
//AClass.cpp
#include "AClass.hpp"
AClass::AClass(void)
{
int a = 1;
if (a++)
{
a = a + 1;
}
else
{
a = a -1;
}
}
AClass::~AClass(void)
{
}
//Reader.hpp
#include <string>
#include "AClass.hpp"
class Reader
{
private:
std::string str_;
//Tokenizer tokenizer_;
AClass ac_;//DELETED TO WORK
public:
Reader(void);
~Reader(void);
std::string read();
};
//Reader.cpp
#include "Reader.h"
#include <iostream>
Reader::Reader(void)
{
}
Reader::~Reader(void)
{
}
std::string Reader::read()
{
std::getline(std::cin,str_);//read input
return str_;//result of calculations is in str_
}
//main.cpp
#include "Reader.h"
int main(int argc, char* argv[])
{
Reader reader;
try
{
reader.read();//BREAKPOINT SET ON THIS LINE IS MARKED BY VS AS UNREACHABLE!!!!!!
}
catch(...)
{
std::cerr << "Something is wrong";
}
return 0;
}

Related

My C++ attempt to convert std::vector<std::string> to JSON doesn't compile

I am trying to port a program I've written in JavaScript to C++ to make it faster. In that program, I dealt somewhat with JSONs. So, I tried to make a method for converting a vector of strings into JSON:
#include <iostream>
#include <vector>
#include <string>
class std::vector<std::string> {
public: std::string JSON();
};
std::string std::vector<std::string>::JSON() {
std::string ret="[";
if (this->size()==0) {
ret+="]";
return ret;
}
int currentSize=this->size();
for (int i=0; i<currentSize; i++) {
if (i!=currentSize-1)
ret+="\""+this->operator[](i)+"\",";
else
ret+="\""+this->operator[](i)+"\"]";
}
return ret;
}
int main(int argc, char **argv) {
std::vector<std::string> fieldOfStrings({"Hello","world","!"});
std::cout <<fieldOfStrings.JSON() <<std::endl;
return 0;
}
However, it doesn't compile:
/home/teo.samarzija/Documents/AECforWebAssembly/AECforWebAssembly.cpp:5:12: error: too few template-parameter-lists
class std::vector<std::string> {
^
/home/teo.samarzija/Documents/AECforWebAssembly/AECforWebAssembly.cpp:9:13: error: specializing member 'std::vector<std::basic_string<char> >::JSON' requires 'template<>' syntax
std::string std::vector<std::string>::JSON() {
What am I doing wrong? I am fairly new to C++.
First, the class std::vector<std::string> does not make sense. Second, a class is not necessary since you can just define a function similar to your member function that takes in std::vector<std::string> and return the json string.
For example:
#include <iostream>
#include <vector>
#include <string>
std::string JSON(std::vector<std::string> str) {
std::string ret="[";
if (str.size()==0) {
ret+="]";
return ret;
}
int currentSize=str.size();
for (int i=0; i<currentSize; i++) {
if (i!=currentSize-1)
ret+="\""+str[i]+"\",";
else
ret+="\""+str[i]+"\"]";
}
return ret;
}
int main(int argc, char **argv) {
std::vector<std::string> fieldOfStrings({"Hello","world","!"});
std::cout <<JSON(fieldOfStrings) <<std::endl;
return 0;
}
As mentioned above the easiest way is a free function.
std::string JSON(std::vector<std::string>& vec) {
std::string ret="[";
if (vec.size()==0) {
ret+="]";
return ret;
}
int currentSize=vec.size();
for (int i=0; i<currentSize; i++) {
if (i!=currentSize-1)
ret+="\""+vec[i]+"\",";
else
ret+="\""+vec[i]+"\"]";
}
return ret;
}
And call it like
std::cout << JSON(fieldOfStrings) <<std::endl;

Printing vector out in functions

I need to print the vector i have filled in listInput. When i go to listPrint the program crashes. What can i do to fix it? Here is my main:
#include <iostream>
#include <string>
#include <vector>
#include "func.h"
using namespace std;
int main(int argc, char** argv) {
subjects a;
int r=1;
while(r!=0){
int select=a.userChoice();
switch(select){
case 1:
a.listPrint();
break;
case 2:
listInput(a);
break;
}
}
return 0;
}
My header:
#ifndef SUBJECT
#define SUBJECT
#include <string>
#include <vector>
class subjects{
private:
std::string subjectName;
std::string lectName;
std::string lectSurname;
int credits;
int studentnum;
public:
/* subjects(){
subjectName="";
lectName="";
lectSurname="";
credits=0;
studentnum=0;
}*/
int userChoice();
int enterNumber(std::string name);
void menu();
std::string getSubjectName(){
return subjectName;
}
std::string getLectName(){
return lectName;
}
std::string getLectSurname(){
return lectSurname;
}
int getCredits(){
return credits;
}
int getStudentNum(){
return studentnum;
}
friend void listInput(subjects a);
void listPrint();
bool checkName(std::string &text);
std::vector<subjects*> entry;
subjects(const std::string subjectName="", const std::string lectName = "", const std::string lectSurname="", const int credits = 0, const int studentnum = 0) :
subjectName(subjectName),
lectName(lectName),
lectSurname(lectSurname),
credits(credits),
studentnum(studentnum){
}
};
#endif
And my function file:
void listInput(subjects a){
.
.
.
a.entry.push_back(new subjects(a.subjectName, a.lectName,a.lectSurname,a.credits, a.studentnum));
}
void subjects::listPrint(){
for(int i=0; i<entry.size(); i++){
cout<<entry[i]->getSubjectName()<<" "<<entry[i]->getLectName()<<" "<<entry[i]->getLectSurname()<<" "<<entry[i]->getCredits()<<" "<<entry[i]->getStudentNum()<<endl;
}
}
I know that using friend functions arent recommended, but i am required to use atleast one of them. Also if i print the vector in listInput, then it only prints the first entry. If there is more than one entry in the vector, it also crashes.
You pass the a instance by value to the list function and then you try to print it. You should consider passing it by reference if you plan to use it outside the scope of the list function.

Clang LibTooling - How to use DependencyCollector

I'm trying to use the DependencyCollector class of Clang in my Tool to list all the dependencies in a file, lets say test.cpp
Here is my program:
#include "stdafx.h"
#include <iostream>
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/CommandLine.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/Utils.h"
using namespace std;
using namespace clang::tooling;
using namespace clang;
using namespace llvm;
static cl::OptionCategory MyToolCategory("my-tool options");
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
static cl::extrahelp MoreHelp("\nMore help text...");
class myDependencyCollector : public DependencyCollector {
private:
public:
bool sawDependency(StringRef Filename, bool FromModule, bool IsSystem, bool IsModuleFile, bool IsMissing) {
if (Filename == "stdafx.h" || IsSystem) {
return false;
} else {
return true;
}
}
bool needSystemDependencies() {
return false;
}
};
class DependencyAction : public PreprocessOnlyAction {
private:
myDependencyCollector *col;
public:
virtual bool usesPreprocessOnly() const {
return true;
}
bool BeginSourceFileAction(CompilerInstance &ci) {
Preprocessor &pp = ci.getPreprocessor();
col = new myDependencyCollector();
col->attachToPreprocessor(pp);
return true;
}
void ExecuteAction() {
}
virtual void EndSourceFileAction() {
llvm::ArrayRef<string> arr = col->getDependencies();
int size = arr.size();
for (int i = 0; i < size; i = i+1) {
cout << arr[i] << endl;
}
}
};
int main(int argc, const char **argv)
{
CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
ClangTool Tool(OptionsParser.getCompilations(), OptionsParser.getSourcePathList());
int result = Tool.run(newFrontendActionFactory<DependencyAction>().get());
return result;
}
Now, if I run the program on, for example the file test.cpp:
#include <iostream>
#include "test.h"
void do_math(int *x) {
*x += 5;
}
int main(void) {
int result = -1, val = 4;
do_math(&val);
return result;
}
The program doesn't find any includes.
It would be great if anybody could help me because I have not been able to find an answer after hours of searching on the internet.
The problem is that you overwrite the ExecuteAction() method from class PreprocessOnlyAction with an empty body.
If you delete the line:
void ExecuteAction() {}
everything works as expected.

Class Creation and use issues

I created a class that represents a packet of information as described on this code:
#ifndef PACKET_H_
#define PACKET_H_
namespace std {
class Packet
{
public:
Packet();
virtual ~Packet();
void initClass();
void setStartP(char);
void setAddFrom(char);
void setAddTo(char);
void setpDataSize(char);
void setpNumber(char);
void setChecksum(char);
void setEndP(char);
void LoadData(char);
char getStartP();
char getAddFrom();
char getAddTo();
char getpDataSize();
char getChecksum();
char getEndP();
char getData();
private:
char pB[261];
char pDataMax;
char pDataIndex;
};
} /* namespace std */
#endif /* PACKET_H_ */
#include "Packet.h"
#include <iostream>
namespace std {
Packet::Packet()
{
pDataIndex = 0;
initClass();
}
Packet::~Packet()
{
delete this;
}
void Packet::setStartP(char startChar)
{
pB[0] = startChar;
cout << "in Set!";
}
void Packet::setAddFrom(char fromChar)
{
}
void Packet::setAddTo(char toChar)
{
}
void Packet::setpDataSize(char dataSizeChar)
{
}
void Packet::setpNumber(char packetNumber)
{
}
void Packet::setChecksum(char checksumChar)
{
}
void Packet::setEndP(char endChar)
{
}
void Packet::LoadData(char dataChar)
{
}
char Packet::getStartP()
{
return pB[0];
cout << "in Get";
}
char Packet::getAddFrom()
{
return pB[1];
}
char Packet::getAddTo()
{
return pB[2];
}
char Packet::getpDataSize()
{
return pB[3];
}
char Packet::getChecksum()
{
return pB[4];
}
char Packet::getEndP()
{
return pB[260];
}
char Packet::getData()
{
return pB[6 + pDataIndex];
}
void Packet::initClass()
{
pDataMax = 254;
pDataIndex = 0;
}
}
At this point i am just testing it so I just implemented two of the methods. When I try to run the program:
#include <iostream>
#include "Packet.h"
using namespace std;
Packet myPacket;
void buildPacket();
int main() {
buildPacket();
return 0;
}
void buildPacket( )
{
char startP = 0x28;
cout << "Setting startP!" << endl;
myPacket.setStartP(startP);
cout << "Getting startP" << endl;
cout << myPacket.getStartP() << endl;
cout << "Done";
}
The code is fine a compile/build time no issues there, it is a run time it falls over. This is really thruowing me, it really is making me doubt what I actually know about class creation and use in C++.
The program will run up to a certain point and then crashes with a windows message. on the console this is as far as it gets before crashing:
Setting startP!
in Set!Getting startP
(
As I can see it it seems to be on deletion that it crashes but not sure why. I looked around for similar issues but can't really find a reason why it is coming up with this, I would be grateful for some help on this one.
Don't call delete this in the destructor. The object is automatically destructed since it goes out of scope, no need for delete.
You can read more about it here: http://en.cppreference.com/w/cpp/language/scope

Incomplete type error when using std::vector with structs

I'm working with c++ STL vectors, and have a vector of structures called projectileList. I'm trying to iterate through the vector, getting and setting values in the struts as I iterate, but my code refuses to compile, with the error 'Incomplete type is not allowed.'
Can anyone please point out what I'm doing wrong:
Code:
ProjectHandeler.h:
#include "stdafx.h"
#include "DataTypes.h"
#include <vector>
class ProjectileHandeler {
private:
int activeObjects;
std::vector<projectile> projectileList;
void projectileUpdater();
public:
ProjectileHandeler(projectile* input[], int projectileCount);
~ProjectileHandeler();
};
#endif
projectileHandeler.cpp
#include "stdafx.h"
#include "DataTypes.h"
#include "ProjectHandeler.h"
#include <vector>
ProjectileHandeler::ProjectileHandeler(projectile* input[], int projectileCount)
{
for (int i = 0; i < projectileCount; i++)
{
projectileList.push_back(*input[i]);
activeObjects += 1;
}
//NO extra slots. Not that expensive.
projectileList.resize(projectileList.size());
}
void ProjectileHandeler::projectileUpdater()
{
while (true)
{
for (unsigned int i = 0; i < projectileList.size(); i++)
{
if (projectileList[i].isEditing == true)
break;
}
}
}
This compiles fine (tested it here: http://codepad.org/cWn6MPJq):
#include <vector>
struct projectile {
bool isEditing;
};
class ProjectileHandeler {
private:
std::vector<projectile> projectileList;
void projectileUpdater()
{
//This bit loops to infinity and beyond! ...or at least untill the handeler is destroyed.
while (true)
{
for (unsigned int i = 0; i < projectileList.size(); i++)
{
if (projectileList[i].isEditing == true) //Throws Incomplete type error
break;
}
}
}
};
int main()
{
}
Notice the removal of *, correct type of loop variable and removal of extra class specifier.