所以当我提出以下内容时,我遇到了这个编译错误:
bash-3.2 $ g ++ -o a * .cpp
架构x86_64的未定义符号: “set_function :: set_function(int)”,引自: _main在ccezs7Gk.o中 ld:找不到架构x86_64的符号 collect2:ld返回1退出状态
但就参考而言,似乎我的文件中的所有内容都是正确的。也许我错过了什么?
//
// hw1_sets.cpp
//
//
#include <iostream>
#include <vector>
#include "hw1.h"
using namespace std;
void set_function::assign() //Assign function
{
cin >> set_function::sets;
cout << set_function::sets << endl;
int i;
if(sets == "R") //if user inputs R
{
for(i=0; i<13; i++) //for loop inputting number into R set
{
cin >> R[i];
// return R[i];
}
}
else if (sets == "S") //if user inputs S
{
for(i=0; i<13; i++) //for loop inputting number into S set
{
cin >> S[i];
// return S[i];
}
}
else if (sets == "T") //if user inputs T
{
for(i=0; i<13; i++) //for loop inputting number into T set
{
cin >> T[i];
// return T[i];
}
}
else
{
cout << "This set does not exist! Try again!" << endl;
}
cout << " set complete" << endl;
};
void set_function::clear() //Clear function
{
//find set
/*cin >> set_function::set;
cout << set_function::set << endl;
int i;
if(set == set_function::R)
{
for(i=0; i<13; i++)
{
//clear R values
}
}
else if (set == set_function.S)
{
for(i=0; i<13; i++)
{
//clear S values
}
}
else if (set == T)
{
for(i=0; i<13; i++)
{
//clear T values
}
}
//remove all values*/
}
void set_function::unionn() //Union function
{
//for loop from 0 to 12 (through all elements)
//if set1[x] or set2[x] = 1
//solution[x]=1
//else
//solution[x]=0
}
void set_function::intersection() //Intersection function
{
//for loop from 0 to 12 (through all elements)
//if set1[x] == set2[x]
//solution[x]=set1[x]
//else
//solution[x]=0
}
void set_function::difference() //difference function
{
//for loop from 0 to 12 (through all elements)
//set1[x]-set2[x]=solution[x]
}
/*printing the set doesn't work*/
void set_function::print() //print function
{
/*string setname;
cin >> setname;
if (setname = "R")
{
for(int i = 0; i<13; i++)
{
cout<< R[i] << "-";
}
cout << endl;
}
else if (setname = "S")
{
for(int i = 0; i<13; i++)
{
cout<< S[i] << "-";
}
cout << endl;
}
else if (setname = "T")
{
for(int i = 0; i<13; i++)
{
cout<< T[i] << "-";
}
cout << endl;
}
//else if lastdigit
//end of command or newline*/
}
//exit
//
// hw1.cpp
//
//
//
//
#include "hw1.h"
#include <iostream>
using namespace std;
int main()
{
string function;
set_function sets(27);
while(1)
{
cout << "sets> ";
cin >> function;
if(function=="assign")
{
sets->assign();
}
else if(function=="clear")
{
sets->clear();
}
else if(function=="union")
{
sets->unionn();
}
else if(function=="intersection")
{
sets->intersection();
}
else if(function=="difference")
{
sets->difference();
}
else if(function=="print")
{
sets->print();
}
else if(function=="quit")
{
// sets->quit();
return 0;
}
else
{
cout<<"error"<<endl;
}
}
}
//
// hw1.h
//
//
//
//
#include <iostream>
using namespace std;
class set_function
{
private:
bool R[13];
bool S[13];
bool T[13];
public:
string sets;
int values[13];
/*void r()
{
R.resize(13);
}
void s()
{
S.resize(13);
}
void t()
{
T.resize(13);
}*/
set_function(int a){}
void assign();
void clear();
void unionn();
void intersection();
void difference();
void print();
// void quit();
};
编辑(10/3/13 @ 12:50p):我改变了评论的内容,现在我遇到了这个问题:
hw1.cpp: In function ‘int main()’:
hw1.cpp:28: error: base operand of ‘->’ has non-pointer type ‘set_function’
hw1.cpp:32: error: base operand of ‘->’ has non-pointer type ‘set_function’
hw1.cpp:36: error: base operand of ‘->’ has non-pointer type ‘set_function’
hw1.cpp:40: error: base operand of ‘->’ has non-pointer type ‘set_function’
hw1.cpp:44: error: base operand of ‘->’ has non-pointer type ‘set_function’
hw1.cpp:48: error: base operand of ‘->’ has non-pointer type ‘set_function’
编辑(10/3/13 @ 1:23p):已修复。改变了以下内容:
set_function *sets = new set_function(27)
到
set_function *sets;
sets = new set_function(27);
现在正确编译。谢谢!
答案 0 :(得分:3)
这不是编译错误,它是链接器错误。您已经获得了它,因为您为set_function
课程声明了构造函数,但没有定义它。
答案 1 :(得分:0)
因此,您在set_function::set_function(int a)
中声明了hw1.h
。然后main.cpp
被正确编译,因为对set_function
的构造函数的调用是正确的,如在头文件hw1.h
中声明的那样。
但该功能未在任何地方实施,发生链接并且未解决呼叫。
您应该在头文件或hw1.cpp
文件中实现它。