I have some code here that doesn't seem to be linking properly. I have searched, and several places have suggested it is a problem with int main(). Not really sure what my problem is. I am pretty new to programming, and I have tried a few different things. Any help would be great!
I have four files: Wire.h, Wire.cpp, Gate.h, and Gate.cpp.
This is the Wire.h
#ifndef WIRE_H
#define WIRE_H
#include <iostream>
#include<vector>
#include<map>
#include<string>
#include "Gate.h"
using namespace std;
class Gate;
class Wire {
public:
// constructors
Wire();
// destructor
~Wire();
//functions
int getState();
void setState(int s);
private:
int State;
vector<Gate*> Gates;
vector<int> History;
};
#endif //WIRE_H
This is Wire.cpp:
#include "Wire.h"
#include<iostream>
using namespace std;
int main() {
cout << "Hello World";
return 0;
}
Wire::Wire(){
State = UNKNOWN;
}
Wire::~Wire(){
for (int i = 0; i < 1/*Gates.size()*/; i++){
Gates.pop_back();
}
for (int i = 0; i < 1/*History.size()*/; i++){
History.pop_back();
}
}
int Wire::getState() {
return State;
}
void Wire::setState(int s) {
State = s;
}
This is Gate.h:
#ifndef GATE_H
#define GATE_H
#include "Wire.h"
#include <iostream>
#include<vector>
#include<map>
#include<string>
using namespace std;
const int HI = 1;
const int LOW = 0;
const int UNKNOWN = -1;
class Wire;
class Gate {
public:
// destructor
~Gate();
//functions
void logic();
void setType(string);
void setDelay(int);
void setAIO(int i, int o); //Only for the NOT gate
void setBIO(int ain, int bin, int o); //For all gates except for NOT
private:
string Type;
Wire* inputA;
Wire* inputB;
Wire* output;
int delay;
};
#endif //GATE_H
This is Gate.cpp
#include "Gate.h"
#include<iostream>
using namespace std;
Gate::Gate(){
inputA = new Wire();
}
Gate::~Gate(){
delete inputA;
delete inputB;
delete output;
}
void Gate::logic(){
if (Type == "NOT"){
if (inputA->getState() == UNKNOWN){
}
if (inputA->getState() == HI){
output->setState(LOW);
}
if (inputA->getState() == LOW){
output->setState(HI);
}
}
if (Type == "AND") {
if (inputA->getState() == HI && inputB->getState() == HI){
output->setState(HI);
}
else {
output->setState(LOW);
}
}
if (Type == "OR") {
if (inputA->getState() == HI || inputB->getState() == HI){
output->setState(HI);
}
else {
output->setState(LOW);
}
}
if (Type == "XOR"){
if (inputA->getState() != inputB->getState()){
output->setState(HI);
}
else
{
output->setState(LOW);
}
}
if (Type == "NAND"){
if (inputA->getState() == HI && inputB->getState() == HI){
output->setState(LOW);
}
else{
output->setState(HI);
}
}
if (Type == "NOR"){
if (inputA->getState() == LOW && inputB->getState() == LOW){
output->setState(HI);
}
else{
output->setState(LOW);
}
}
if (Type == "XNOR"){
if (inputA->getState() == inputB->getState()){
output->setState(HI);
}
else
{
output->setState(LOW);
}
}
}
void Gate::setType(string t){
Type = t;
}
void Gate::setDelay(int d){
delay = d;
}
In c++, when compiling an executable, the compiler needs to know where to start execution. In order to make this happen, you need a function called main with this signature:
int main() { ... }
OR
int main(int argc, char** argv){ ... }
You don't have this function. Add it to one of your cpp files.
Related
I am trying to find the minimum vertex cover by giving the vertex and edge input in specific format from the user using threads. Here is my code:
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <cctype>
#include <list>
#include <set>
#include <vector>
#include <climits>
#include <memory>
#include <algorithm>
#include <pthread.h>
#include <unistd.h>
#include "minisat/core/Solver.h"
using namespace std;
static void *AVC2_Vertex_Cover(void *);
void min_vertex_cover_algorithm(Graph &graph_builder){
int ret;
pthread_t AVC2_thread;
ret = pthread_create(&AVC2_thread, NULL, AVC2_Vertex_Cover, &graph_builder);
if(ret)
exit(1);
pthread_join(AVC2_thread, NULL);
pthread_exit(NULL);
}
struct Edge{
unsigned v1,v2;
};
typedef std::vector<unsigned> Vertex_vector;
typedef std::list<unsigned > Vertex_Adjacency_list;
typedef std::vector<Vertex_Adjacency_list> Adjacency_Traversal_list;
struct Graph{
std::size_t no_of_edges;
Adjacency_Traversal_list adjacency_list;
void initialize_graph(unsigned vertices_number);
void construct_edge(Edge edge);
void clear(unsigned vertex);
};
void Graph::initialize_graph(unsigned num){
adjacency_list.clear();
no_of_edges = 0;
adjacency_list.resize(num,{});
}
void Graph::construct_edge(Edge edge) {
auto &literal_1 = adjacency_list[edge.v1];
auto &literal_2 = adjacency_list[edge.v2];
literal_1.push_back(edge.v2);
literal_2.push_back(edge.v1);
no_of_edges ++;
}
void *AVC2_Vertex_Cover(void *input)
{
Graph g = *(const Graph *)input;
unsigned int V = g.adjacency_list.size();
bool visited[V];
for (int i=0; i<V; i++)
visited[i] = false;
for (int u=0; u<V; u++)
{
if (visited[u] == false)
{
for(int x : g.adjacency_list[u])
{
int v = x;
if (visited[v] == false)
{
visited[v] = true;
visited[u] = true;
break;
}
}
}
}
// Print the vertex cover
std::cout << "APPROX-VC-2: ";
for (int i=0; i<V; i++){
if (visited[i])
if(i == V-1)
cout << i << std::endl;
else
cout << i << ",";
}
}
void *IO_thread(void *)
{
Graph &graph_builder = *new Graph();
char character_input;
string my_input;
unsigned int no_of_vertices = 0;
string edge_stream;
char prev_choice = ' ';
while (getline(cin, my_input))
{
istringstream stream_string(my_input);
while (stream_string >> character_input)
{
character_input=(toupper(character_input));
try
{
switch (character_input)
{
case 'V' :
if (prev_choice == 'V')
{
cerr << "Error: V must be followed by E only.\n";
break;
}
else
{
stream_string >> no_of_vertices;
if(no_of_vertices <= 0)
{
throw "Invalid number of vertices";
}
graph_builder.initialize_graph(no_of_vertices);
prev_choice = 'V';
break;
}
case 'E' :
{
unsigned int flag_Entry = 0;
if ( prev_choice == 'E')
{
cerr << "Error: V and E always occur together.\n ";
break;
}
else
{
stream_string >> edge_stream;
istringstream edge_stream_character(edge_stream);
char edg_char;
unsigned int temp = 0;
unsigned int v1;
unsigned int v2;
edge_stream_character >> edg_char;
while (edg_char != '}')
{
edge_stream_character >> edg_char;
if (edg_char == '}')
{
flag_Entry = 1;
break;
}
else
{
edge_stream_character >> temp;
v1 = temp;
edge_stream_character >> edg_char;
edge_stream_character >> temp;
v2 = temp;
edge_stream_character >> edg_char;
edge_stream_character >> edg_char;
if (v1 >= no_of_vertices || v2 >= no_of_vertices)
{
cerr << "Error: Vertex out of range.\n";
graph_builder.adjacency_list.clear();
break;
}
graph_builder.construct_edge({v1,v2});
}
}
if(flag_Entry == 1)
{
prev_choice = 'E';
break;
}
min_vertex_cover_algorithm(graph_builder);
prev_choice = 'E';
break;
}
}
}
}
catch (const char* err)
{
cerr << "Error:" << err << endl;
}
}
}
return 0;
}
int main(int argc, char **argv){
int ret;
pthread_t IO_thread;
ret = pthread_create(&IO_thread, NULL, IO_thread,NULL);
if(ret)
return 1;
pthread_join(IO_thread,NULL);
pthread_exit(NULL);
}
I am getting an error:
unknown type name 'Graph'
void min_vertex_cover_algorithm(Graph &graph_builder){
I am not able to find why this error is occuring. It will be very helpful if I get some solutions.
Just like you, the compiler will read from top to bottom. When it reaches the line:
void min_vertex_cover_algorithm(Graph &graph_builder){
It has to go, ok, lets use a Graph reference. It will look for the declaration of a Graph, which it cannot find, because you have declared (and defined) it below. To solve this, give the compiler a hint. Declare the Graph above the function with:
struct Graph;
void min_vertex_cover_algorithm(Graph &graph_builder){
Or simply move the whole struct definition above the function.
Here I have an array within the private section of my class which is unable to retain the values set in different members of the clas; during the fight sequence both of the values in the array equate to 0.
#include <iostream>
#include <string>
#include<math.h>
#include<cstdlib>
using namespace std;
class champions {
private:
int health_array[2] = { };
int attack_array[2] = { };
public:
void health_attack_set() {
int health_set{};
int attack_set{};
for (int i = 0; i < 2; i++) {
health_array[i] = health_set;
attack_array[i] = attack_set;
}
}
void fight_sequence(int random_champion, int random_opponent) {
cout << "FIGHT\n\n";
while (health_array[random_champion] > 0 or health_array[random_opponent] > 0) {
(health_array[random_opponent] -= attack_array[random_champion]);
if (health_array[random_opponent] <= 0) {
break;
}
(health_array[random_champion] -= attack_array[random_opponent]);
}
if (health_array[random_champion] > 0) {
cout << "CHAMPION 1 WINS!!!";
}
if (health_array[random_opponent] > 0) {
cout << "CHAMPION 2 WINS!!!";
}
if (health_array[random_champion] == 0 && health_array[random_opponent] == 0) {
cout << "NO ONE WINS!!";
}
}
void champion_1() {
health_attack_set();
health_array[0] = 400;
attack_array[0] = 150;
}
void champion_2() {
health_attack_set();
health_array[1] = 500;
attack_array[1] = 100;
}
};
int main() {
champions fight;
fight.fight_sequence(0, 1);
return 0;
}
I believe it may be a simple mistake but hard to spot; thank you for any help that is given.
I think your problems are coming from not understanding how to structure your code in the presence of classes and multiple objects. I have restructured your code below in a cleaner and more common way (it is not the only way to do it, but it is more typical). Hopefully there are not typos.
#include <iostream>
#include <string>
#include<math.h>
#include<cstdlib>
using namespace std;
class Champion{
public:
int health;
int attack;
Champion(int health_, int attack_) : health(health_), attack(attack_) {
}
};
void fight_sequence(Champion& champion, Champion& opponent) {
cout << "FIGHT\n\n";
while (champion.health > 0 || opponent.health > 0) {
(opponent.health -= champion.attack);
if (opponent.health <= 0) {
break;
}
(champion.health -= opponent.attack);
}
if (champion.health > 0) {
cout << "CHAMPION 1 WINS!!!";
}
if (opponent.health > 0) {
cout << "CHAMPION 2 WINS!!!";
}
if (champion.health == 0 && opponent.health == 0) {
cout << "NO ONE WINS!!";
}
}
int main() {
Champion champion_1(400, 150);
Champion champion_2(500, 100);
fight_sequence(champion_1, champion_2);
return 0;
}
I am to create a custom String class in C++ with different methods in it.
The problem I have is that I get a "Use of undeclared identifier" error with a method that is declared in another file:
#include <iostream>
#include "String.h"
int main() {
std::cout << "Hello World!\n";
char str[] = "Today I am happy.";
cout<<strlen(str); // error
strlwr(str); //error
}
This is the other file, String.h:
#include <iostream>
using namespace std;
class Strings
{
public:
char *s;
Strings(char *a){
int l=0;
for(int i=0;a[i]!='\0';i++)
l++;
/*The length of string a*/
s=new char[l+1];
for(int i=0;a[i]!='\0';i++)
s[i]=a[i];
}
void strlwr(char *s1){ //method
for(int i=0;s1[i]!='\0';i++)
{
if(s1[i]>='A' && s1[i]<='Z')
s1[i]=(char)(s1[i]+32);
}
for(int i=0;s1[i]!='\0';i++)
cout<<s1[i];
}
int strlen(char *s1) //method
{
int l=0;
for(int i=0;s1[i]!='\0';i++)
{
l++;
}
return l;
}
int strcmp(char *s1,char *s2){
while(*s1 != '\0' && *s2 != '\0' && *s1 == *s2){
s1++;
s2++;
}
if(*s1 == *s2){
cout<<"They are equal";
return 0;
}
else if(*s1 >= *s2)
return 1;
else
return -1;
}
};
This has worked fine with other programs. I don't know why I have this problem now.
If these functions are independent of the class, you can declare them static.
static void strlwr(char *s1){ //method
static int strlen(char *s1) //method
And them use them like this:
cout<<String::strlen(str); // error
String::strlwr(str); //error
can someone give me tricky test example that crash my code. Task --> http://www.spoj.com/problems/SHOP/
I can't find any mistake in my code so I am asking your help.
Code:
http://pastebin.com/6wuFWWJH
#include <stdio.h>
#include <queue>
#include <algorithm>
using namespace std;
struct koord
{
int x;
int y;
koord(int _x=0,int _y=0)
{
x=_x;
y=_y;
}
};
queue<koord>Q;
bool bio[150][150];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int dist[150][150];
char polje[150][150];
int a,b;
void bfs(int a1,int b1)
{
Q.push(koord(a1,b1));
bio[a1][b1]=true;
while(!Q.empty())
{
koord pos=Q.front();
Q.pop();
for(int i=0;i<4;++i)
{
koord dalje=koord(pos.x+dx[i],pos.y+dy[i]);
if(dalje.x>=0 && dalje.x<b && dalje.y>=0 && dalje.y<a)
{
if(polje[dalje.x][dalje.y]!='X' || polje[dalje.x][dalje.y]!='S')
if(bio[dalje.x][dalje.y]==false)
{
if(polje[dalje.x][dalje.y]=='D')
{
bio[dalje.x][dalje.y]=true;
dist[dalje.x][dalje.y]=dist[pos.x][pos.y];
Q.push(dalje);
}
else
{
bio[dalje.x][dalje.y]=true;
dist[dalje.x][dalje.y]=dist[pos.x][pos.y]+(polje[dalje.x][dalje.y]-'0');
Q.push(dalje);
}
}
if(polje[dalje.x][dalje.y]=='D' && dist[dalje.x][dalje.y]>dist[pos.x][pos.y])
{
dist[dalje.x][dalje.y]=dist[pos.x][pos.y];
Q.push(dalje);
}
else if(dist[dalje.x][dalje.y]>dist[pos.x][pos.y]+(polje[dalje.x][dalje.y]-'0'))
{
dist[dalje.x][dalje.y]=dist[pos.x][pos.y]+(polje[dalje.x][dalje.y]-'0');
Q.push(dalje);
}
}
}
}
}
int main()
{
scanf("%d%d",&a,&b);
while(a!=0 && b!=0)
{
int c=0,d=0,e=0,f=0;
for(int i=0;i<b;++i)scanf("%s",polje[i]);
//scanf("\n");
for(int i=0;i<b;++i)
for(int j=0;j<a;++j)
{
if(polje[i][j]=='S'){c=i;d=j;}
if(polje[i][j]=='D'){e=i;f=j;}
}
bfs(c,d);
printf("%d\n",dist[e][f]);
for(int i=0;i<150;++i)
for(int j=0;j<150;++j){bio[i][j]=false;dist[i][j]=0;}
Q.empty();
scanf("%d%d",&a,&b);
}
return 0;
}
I try some weird test example like :
3 3
S9D
1X1
111
But my program print 5, and that is good. So I need your help.
Mistake was here:
if(polje[dalje.x][dalje.y]!='X' || polje[dalje.x][dalje.y]!='S')
It should be
if(polje[dalje.x][dalje.y]!='X' && polje[dalje.x][dalje.y]!='S')
I wrote this program and it supposed to test for the correct use of the three grouping symbols "(",")";"[","]"; and "{","}". It is using the array implementation of the stacks and supposed to evaluate if it is good string or a bad string. For example: (a+b), [(a-b)+c] would be good and )a+b( etc. would be bad string. When i run the program i get only one error. I thought i am missing a semi-colon or something, but after looking through the code several time,i can't find it. Maybe i got tunnel vision. Can you please see what the problem here is? This is the error: project1.cpp:41: error: expected initializer before 'while'.
#include <string>
#include <iostream>
#include <stdio.h>
using namespace std;
const int DefaultListSize = 100;
typedef char Elem;
class Astack {
private:
int size;
int top;
Elem *listArray;
public:
Astack (int sz = DefaultListSize)
{size = sz; top= 0; listArray = new Elem[sz];}
~Astack() {delete [] listArray;}
void clear() {top=0;}
bool push(const Elem& item) {
if (top == size) return false;
else {listArray[top++] = item; return true;}}
bool pop(Elem& it) {
if (top==0) return false;
else {it = listArray[--top]; return true;}}
bool topValue(Elem& it) const {
if (top==0) return false;
else {it = listArray[top-1]; return true;}}
bool isEmpty() const {if (top==0) return true;
else return false;}
int length() const{return top;}
}; //end of class Astack
Astack s;
const string LEFTGROUP="([{";
const string RIGHTGROUP=")]}";
int main()
while (!EOF) {
while (!EOL) {
ch = getc();
if (ch == LEFTGROUP[0]) {
s.push(ch);
}
if (ch == LEFTGROUP[1] {
s.push(ch);
}
if (ch == LEFTGROUP[2] {
s.push(ch);
}
} //checking for openers
while (!EOL) {
ch = getc();
if (s.top() == LEFTGROUP[0]) {
if (ch == RIGHTGROUP[0]) {
s.pop();
}
}
if (s.top() == LEFTGROUP[1]) {
if (ch == RIGHTGROUP[1]) {
s.pop();
}
}
if (s.top() == LEFTGROUP[2]) {
if (ch == RIGHTGROUP[2]) {
s.pop();
}
}
if (!s.empty()) {
cout<<"Bad String."<<endl;
else {
cout<<"Good String."endl;
}
}
}
return 0;
You forgot a { at the beginning of int main(). You should also end with }
int main(){
//your while code
return 0;
}