#include <iostream>
#include <stdlib.h>
class circuitTypes{
protected:
static int size;
circuitTypes **Matrix;
int input1,input2;
int output1, output2;
public:
circuitTypes() {};
static int getSize() { return size; };
static void upSize() { size++; };
void ItemRegistry();
virtual void setTruthTable()=0;
void setInputAndCalculateOutput(int a, int b);
int *getOutput();
};
int circuitTypes::size=0;
int *circuitTypes::getOutput(){
int Output[2];
Output[0]=output1;
Output[1]=output2;
return Output;
}
void circuitTypes::ItemRegistry(){
circuitTypes::upSize();
int circuitSize=circuitTypes::getSize();
if(circuitSize==1)
Matrix=(circuitTypes **)malloc(circuitSize*sizeof(circuitTypes *));
else
Matrix=(circuitTypes **)realloc(Matrix,circuitSize*sizeof(circuitTypes *));
if(Matrix==0){
std::cout <<"No available memory \n";
exit(1);
}
Matrix[circuitSize-1]=this;
}
void circuitTypes::setInputAndCalculateOutput(int a, int b){
input1=a;
input2=b;
setTruthTable();
}
class TypeA : private circuitTypes{
public:
TypeA() { ItemRegistry(); };
void setTruthTable();
};
void TypeA::setTruthTable(){
if (input1==0){
if (input2==0){
output1=0;
output2=0;
}
else{
output1=0;
output2=1;
}
}
else{
if (input2==0){
output1=0;
output2=1;
}
else{
output1=1;
output2=1;
}
}
}
class TypeB : private circuitTypes{
public:
TypeB() { ItemRegistry(); };
void setTruthTable();
};
void TypeB::setTruthTable(){
if (input1==0){
if (input2==0){
output1=0;
output2=0;
}
else{
output1=0;
output2=1;
}
}
else{
if (input2==0){
output1=1;
output2=1;
}
else{
output1=0;
output2=1;
}
}
}
void circuit (circuitTypes **Example, int a, int b){
std::cout << "NIKKK";
Example[0]->setInputAndCalculateOutput(a,b);
int Size=Example[0]->getSize();
for (int i=1;i<Size;i++){
Example[i]->setInputAndCalculateOutput(Example[i-1]->getOutput()[0],Example[i-1]->getOutput()[1]);
}
std::cout << "For input a= " << a << " and b= " << b << " the result is c= " << Example[Size-1]->getOutput()[0] << " and d=" << Example[Size-1]->getOutput()[1] << "\n";
}
int main (){
circuitTypes **Example;
TypeA A1,A2,A3;
TypeB B1,B2,B3;
for (int i=0;i<2;i++){
for (int j=0;j<2;j++){
circuit (Example,i,j);
}
}
}
我很抱歉代码的大小,但由于我不知道问题出在哪里,我决定发布整个程序。在Ubuntu 11.10(使用g ++)中编译.cpp文件时没有任何问题但是当我尝试执行a.out文件时出现此错误:
*检测到glibc ./a.out:realloc():指针无效:0x003c2ff4 * *
======= Backtrace:=========
/lib/i386-linux-gnu/libc.so.6(+ 0x721a2)[0x2b71a2]
/lib/i386-linux-gnu/libc.so.6(realloc的+ 0x2a5)[0x2bb245]
./ a.out的[0x8048835]
./ a.out的[0x8048ca9]
./ a.out的[0x8048b2a]
/lib/i386-linux-gnu/libc.so.6(__ libc_start_main + 0xf3)[0x25e113]
./ a.out的[0x80486c1]
=======记忆图:========
00110000-0012c000 r-xp 00000000 08:06 1311674 /lib/i386-linux-gnu/libgcc_s.so.1
0012c000-0012d000 r - p 0001b000 08:06 1311674 /lib/i386-linux-gnu/libgcc_s.so.1
等
任何人都可以帮助我吗?
答案 0 :(得分:0)
if(circuitSize==1)
Matrix=(circuitTypes **)malloc(circuitSize*sizeof(circuitTypes *));
else
Matrix=(circuitTypes **)realloc(Matrix,circuitSize*sizeof(circuitTypes *));
我不明白这里的if
条件。当然应该是if (Matrix == 0)
?否则,你冒着尝试realloc
首先从未malloc
编辑过的东西的风险。
但我同意@JerryCoffin你应该抛弃它并使用std::vector
。
答案 1 :(得分:0)